我有一个 xaml 结构,看起来像这样:
<StackPanel>
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="200" />
</Style>
</TextBox.Style>
</TextBox>
<ToolBarPanel Orientation="Horizontal">
<Button Content="Funky stuff" Command="{Binding Query}" CommandParameter="{what to type in here?}" />
</ToolBarPanel>
</StackPanel>
如果用户单击 ToolBarPanel
下的 Button
,我希望将 TextBox
的内容作为 CommandParameter
传递>。那么如何找到这个元素呢?
最佳答案
您可以命名文本框,然后将命令参数绑定(bind)到文本属性:
<StackPanel>
<TextBox x:Name="myText">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="200" />
</Style>
</TextBox.Style>
</TextBox>
<ToolBarPanel Orientation="Horizontal">
<Button Content="Funky stuff"
Command="{Binding Query}"
CommandParameter="{Binding Text, ElementName=myText}" />
</ToolBarPanel>
</StackPanel>
希望这对您有所帮助。
关于c# - 在 WPF 控件中查找同级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23042146/