I want to create element dynamically, so I use C# code behide to create a control.
Radio button was created and I want to bind some element with it (in this case, I use button).
Here is my source code
/*create radio button */
RadioButton secondaryRadio = new RadioButton()
{
Name = "secondaryRadio_" + orderOfTransport + "_" + orderOfSubTransport,
GroupName = "Transport_" + orderOfTransport + "_" + orderOfSubTransport,
IsChecked = false,
};
/*create bind object */
Binding userChoice2 = new Binding("IsChecked")
{
ElementName = "secondaryRadio_" + orderOfTransport + "_" + orderOfSubTransport,
};
/*create button and bind */
Button outBoundButton = new Button()
{
Content = "Select",
Name = "inb_button_" + orderOfTransport + "_" + orderOfSubTransport,
};
outBoundButton.SetBinding(Button.IsEnabledProperty, userChoice2);
and this is what got from Output window
Cannot find source for binding with reference 'ElementName=secondaryRadio_1_0'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'Button' (Name='inb_button_1_0'); target property is 'IsEnabled' (type 'Boolean')
What I did wrong for this binding? Can I use binding object more than 1 time?
Many thanks for your help :D
"secondaryRadio_1_0"anywhere in the UI. Is that radio button added to the UI after its created? Other things to check is that the radio button exists somewhere in the VisualTree that the Button can access, and you may need to make sure the radio button is rendered prior to the binding being evaluated.What I did wrong for this binding?- Everything. Don't create or manipulate UI elements in code. WPF is not winforms. Learn MVVM. - Disclaimer: sorry for the rudeness and for being so straightforward, I'm just listening to Pantera.