我使用以下标记来定义我的控件。我已经看到了声明我已经完成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>
我正在尝试使用后面的代码中的以下例程来访问控件。我来自winforms背景,请原谅我,如果这不是正确的方法。
private void addColumn(object sender, RoutedEventArgs e)
{
_ColumnsModeList.Add(new CustomColumnsModel
{
DisplayNameOverride =txtDsiplayName.Text,
Width =100,
Order=1
});
}
答案 0 :(得分:0)
你可以通过他的名字访问任何控件,例如:
<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>
在您的代码后面的
表示控件的名称,在本例中为 listView 。