Marek M.
Marek M.

Reputation: 3951

Find sibling in WPF control

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>

If user clicks the Button under ToolBarPanel, I want the content of TextBox to be passed as the CommandParameter. So how do I find this element?

Upvotes: 4

Views: 2250

Answers (1)

Dmitry
Dmitry

Reputation: 2052

You can name your Textbox and then bind you command parameter to the text property:

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

Hope this helps.

Upvotes: 3

Related Questions