[英]Can't access WPF user control in Code Behind
I am using the following markup to define my controls. 我正在使用以下标记来定义我的控件。 I have seen various other examples of the point of declaring the x:Name
attribute which I have done, but I still cannot select my control in the code behind. 我已经看到了声明x:Name
属性的其他示例,但是仍然无法在后面的代码中选择控件。
<UserControl x:Class="AMTR2.UI.Controls.CustomColumnsBuilderControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="332" d:DesignWidth="1038">
<Canvas>
<ListView Canvas.Left="12" Canvas.Top="48" Height="183" Name="listView1"
Width="453"
SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Order" Width="100"
DisplayMemberBinding="{Binding Col1}"></GridViewColumn>
<GridViewColumn Header="Display Name" Width="290"
DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
<GridViewColumn Header="Width" Width="50"
DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Name="moveUpButton" Canvas.Left="472.4" Canvas.Top="50" Content="Move Up"
Height="22" Width="74" IsEnabled="False" />
<Button Name="moveDownButton" Canvas.Left="472.4" Canvas.Top="80" Content="Move Down"
Height="22" Width="74" IsEnabled="False" />
<Button Name="deleteButton" Canvas.Left="472.4" Click="removeButton_Click" Canvas.Top="110" Content="Delete"
Height="22" Width="74" />
<Button Name="addButton" Click="addColumn" Canvas.Left="472.4" Canvas.Top="140" Content="Add Item"
Height="22" Width="74" />
<Label Content="Name:" Canvas.Left="12" Canvas.Top="250" />
<TextBox x:Name="txtDsiplayName" Canvas.Left="12" Canvas.Top="280" Height="23"
Width="450.4" />
<Label Content="Width:" Canvas.Left="470" Canvas.Top="250" />
<TextBox x:Name="txtWdith" Canvas.Left="470" Canvas.Top="280" Height="23"
Width="70.4"
/>
</Canvas>
</UserControl>
I am trying to access the control using the following routine in my code behind. 我试图在后面的代码中使用以下例程访问该控件。 I am coming from a winforms background so excuse me if this is not the correct way to go about it. 我来自winforms背景,所以请问这不是正确的解决方法。
private void addColumn(object sender, RoutedEventArgs e)
{
_ColumnsModeList.Add(new CustomColumnsModel
{
DisplayNameOverride =txtDsiplayName.Text,
Width =100,
Order=1
});
}
you can access any control by his name, for exemple: 您可以按他的名字访问任何控件,例如:
<UserControl x:Class="AMTR2.UI.Controls.CustomColumnsBuilderControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="332" d:DesignWidth="1038">
<Canvas>
<ListView Name="listView" Canvas.Left="12" Canvas.Top="48" Height="183" Name="listView1"
Width="453"
SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Order" Width="100"
DisplayMemberBinding="{Binding Col1}"></GridViewColumn>
<GridViewColumn Header="Display Name" Width="290"
DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
<GridViewColumn Header="Width" Width="50"
DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Name="moveUpButton" Canvas.Left="472.4" Canvas.Top="50" Content="Move Up"
Height="22" Width="74" IsEnabled="False" />
<Button Name="moveDownButton" Canvas.Left="472.4" Canvas.Top="80" Content="Move Down"
Height="22" Width="74" IsEnabled="False" />
<Button Name="deleteButton" Canvas.Left="472.4" Click="removeButton_Click" Canvas.Top="110" Content="Delete"
Height="22" Width="74" />
<Button Name="addButton" Click="addColumn" Canvas.Left="472.4" Canvas.Top="140" Content="Add Item"
Height="22" Width="74" />
<Label Content="Name:" Canvas.Left="12" Canvas.Top="250" />
<TextBox x:Name="txtDsiplayName" Canvas.Left="12" Canvas.Top="280" Height="23"
Width="450.4" />
<Label Content="Width:" Canvas.Left="470" Canvas.Top="250" />
<TextBox x:Name="txtWdith" Canvas.Left="470" Canvas.Top="280" Height="23"
Width="70.4"
/>
</Canvas>
in your code behind you indicate the name of the control, in this case listView . 在后面的代码中,指示控件的名称,在本例中为listView 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.