c# - 在 WPF 控件中查找同级

标签 c# wpf

我有一个 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/

相关文章:

c# - WPF 应用程序 mp3 声音文件的相对路径

c# - 非用户文本的文字或资源文件

c# - 在 C#/WinForms 中制作多语言应用程序的最佳实践?

wpf - WPF中带有MVVM的ICommand

wpf - 如何使 ItemsControl 根据运行时类型选择不同的模板

wpf - 打开大数据弹出窗口的最佳方法

c# - 不带主键的 LINQ DeleteOnSubmit

c# - 为什么下载后文件类型变了?

c# - 无法将子字符串发送到数据库

c# - 关于在 WPF 中的 Canvas 中移动形状的一些事情