在WPF控件中查找兄弟控件

4

I have a xaml structure, that looks like this:

<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传递。那么如何找到这个元素?
1个回答

3
您可以为您的文本框命名,然后将您的命令参数绑定到文本属性:

<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>

希望这能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接