我有一个动态布局窗口应用程序,通过更改组合框中的值,文本框中的值也应该更改。
文本框是网格的一部分,在<window.resources>
中定义,并在运行时时添加到可视树中:
grpEditFrame.Content = this.Resources["grdARP"] as Grid; // the text box is inside this grid
所以这就是我所做的:
((grpEditFrame.Content as Grid).
FindName("txtHardwareType") as TextBox).Text= "some text"
问题是由于某些原因FindName
找不到文本框。通过调试我看不到它们存在,但FindName
只是不起作用:(
所以在Google中查看我发现问题是在运行时创建的控件,它们的名称没有自动注册,所以程序员必须自己完成。但后来我找到了this。
因此,如果控件是由XAML创建的,就像我所做的那样(在<windows.resources>
中定义它们)那么问题是什么?
此外,如果我没错,那么<windows.resources>
中的控件正在编译时创建!
要么为什么,我认为名字必须已经注册,为什么找不到它们?
这是我上面网格的<windows.resources>
定义:
<Grid x:Key="grdARP" HorizontalAlignment="Right" Width="572">
<Label Content="Destination MAC:" HorizontalAlignment="Left" Margin="74,18,0,0" VerticalAlignment="Top"/>
<Label Content="Source MAC:" HorizontalAlignment="Left" Margin="199,18,0,0" VerticalAlignment="Top"/>
<Label Content="Ethernet Type:" HorizontalAlignment="Left" Margin="319,18,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="74,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" MaxLength="12"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="199,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" MaxLength="12"/>
<TextBox Name="txtEthernetType" HorizontalAlignment="Left" Height="23" Margin="324,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="100" MaxLength="4"/>
<Label Content="0x0000" HorizontalAlignment="Left" Margin="21,41,0,0" VerticalAlignment="Top"/>
<TextBox Name="txtHardwareType" HorizontalAlignment="Left" Height="23" Margin="429,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="110" MaxLength="2"/>
<Label Content="Hardeware type:" HorizontalAlignment="Left" Margin="429,18,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.359,0.5"/>
<Label Content="Protocol type:" HorizontalAlignment="Left" Margin="69,72,0,0" VerticalAlignment="Top"/>
<Label Content="0x0010" HorizontalAlignment="Left" Margin="21,98,0,0" VerticalAlignment="Top"/>
<TextBox Name="txtProtocolType" HorizontalAlignment="Left" Height="23" Margin="74,98,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" MaxLength="4"/>
<Label Content="Hardware size:" HorizontalAlignment="Left" Margin="150,72,0,0" VerticalAlignment="Top"/>
<TextBox Name="txtHardwareSize" HorizontalAlignment="Left" Height="23" Margin="155,98,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="78" MaxLength="2"/>
<Label Content="Protocol size:" HorizontalAlignment="Left" Margin="234,72,0,0" VerticalAlignment="Top"/>
<TextBox Name="txtProtocolSize" HorizontalAlignment="Left" Height="23" Margin="238,98,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="73" MaxLength="2"/>
<Label Content="Opcode:" HorizontalAlignment="Left" Margin="311,72,0,0" VerticalAlignment="Top"/>
<TextBox Name="txtOpcode" HorizontalAlignment="Left" Height="23" Margin="316,98,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="50" MaxLength="4"/>
<Label Content="Sender MAC:" HorizontalAlignment="Left" Margin="366,72,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="371,98,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="93" MaxLength="12"/>
<Label Content="Sender IP:" Height="26" VerticalAlignment="Top" Margin="459,72,44,0"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="464,98,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="75" MaxLength="8"/>
<Label Content="0x0020" HorizontalAlignment="Left" Margin="21,157,0,0" VerticalAlignment="Top"/>
<Label Content="Target MAC:" HorizontalAlignment="Left" Margin="74,134,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="74,160,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="102" MaxLength="12"/>
<Label Content="Target IP:" HorizontalAlignment="Left" Margin="181,134,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="181,160,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" MaxLength="8"/>
<Label Content="RAW: (optional)" HorizontalAlignment="Left" Margin="306,134,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="306,160,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="233" MaxLength="12" ToolTip="Any data put here has no effect"/>
<Label Content="0x0030" HorizontalAlignment="Left" Margin="21,216,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Margin="74,216,0,10" TextWrapping="Wrap" Text="" Width="465"/>
<Label Content="RAW : (optional)" HorizontalAlignment="Left" Margin="74,190,0,0" VerticalAlignment="Top" ToolTip="Any data put here has no effect"/>
</Grid>
这是我的C#代码隐藏功能:
private void cmdArpHardwareTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//set the hardware type in txtHardwareType text box
((grpEditFrame.Content as Grid).
FindName("txtHardwareType") as TextBox).Text = ((Int16)Enum.Parse(
typeof(ArpHardwareTypes),
(sender as ComboBox).SelectedItem.ToString()))
.ToString("x4");//4 chars hex format
//set hardware size to 6 only if harware type is ethernet
if ((sender as ComboBox).SelectedItem.ToString() == ArpHardwareTypes.Ethernet10MB.ToString())
((grpEditFrame.Content as Grid).
FindName("txtHardwareType") as TextBox).Text = ArpHardwareSize.Ethernet.ToString();
}
Hoplese
建议添加UpdateLayout,这就是我所做的(并且它不起作用......)
void cmbEthernetIItypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//clear the editf frame grid
grpEditFrame.Content = null;
//clear son of parent (trviEthernetII)
(trviRoot.Items.GetItemAt(0)/*trivEtherntII*/ as TreeViewItem).Items.Clear();
switch ((sender as ComboBox).SelectedItem.ToString())
{
case "RAW":
grpEditFrame.Content = this.Resources["grdEthernetIIraw"] as Grid;
grpEditFrame.UpdateLayout();
grpEditFrame.InvalidateArrange();
grpEditFrame.InvalidateMeasure();
grpEditFrame.InvalidateVisual();
break;
case "ARP":
//set the edit grpup box
grpEditFrame.Content = this.Resources["grdARP"] as Grid;
grpEditFrame.UpdateLayout();
grpEditFrame.InvalidateArrange();
grpEditFrame.InvalidateMeasure();
grpEditFrame.InvalidateVisual();
//set the tree view item
(trviRoot.Items.GetItemAt(0)/*trivEtherntII*/ as TreeViewItem).Items.Add(this.Resources["trviARP"]);
trviRoot.UpdateLayout();
break;
default:
break;
}
}