How I should reference Custom Control in C# code behind file event trigger?
In Xaml page I reference my custom control this way:
<MyNamespace:CustControl x:Name="Cust1" />
Now I want change the property of this custom control in MouseLeftButtonDown event trigger:
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
But when I try to write something like
CustControl.IsSelected = true;
An error says: An object reference is required ..
I think it's all about "MyNamespace" namespace, but don't know开发者_JAVA技巧 how to reference it.
You should be referencing Cust1, but sometimes Visual Studio does not immediately create a field member for the control. Try typing Cust1.IsSelected and even if Visual Studio doesn't like it, try building to see if it succeeds.
The x:Name is "Cust1", so you would refer to it as Cust1:
Cust1.IsSelected = true;
I.e. Cust1 is the name of the instance; CustControl is the name of the type.
CustControl
is the name of the class; Cust1
is the name of the instance.
Try Cust1.IsSelected = true
.
You would reference is as "Cust1", not its type.
Cust1.IsSelected = true;
try:
Cust1.IsSelected = true;
where "Cust1" is the Name property of the control
If WPF & asp.net work the same way "CustControl" is the name of the class, "Cust1" is the instance.
精彩评论