Search code examples
c#wpfdata-bindingbind

How to bind a template element property in DataTemplate


I want to let a template element property create a binding with another element property, but I found a lot articles and none of them talk about. So I ask for what should I do.

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:StringsJoinConverter x:Key="join_converter" />
    </Window.Resources>

    <Grid DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <ListBox ItemsSource="{Binding Collection}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Background="#FF83C9A9" >
                                <TextBlock Text="Name:"> </TextBlock>
                                <TextBox Width="50" x:Name="input_name"></TextBox>
                            </TextBlock>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Background="#FFB683C9" >
                                <TextBlock Text="Value:"> </TextBlock>
                                <TextBox Width="50" x:Name="input_value"></TextBox>
                            </TextBlock>
                        </StackPanel>

                        <TextBlock >
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource join_converter}">
                                    <Binding>
                                        <!--Bind input_name.Text-->
                                    </Binding>
                                    <Binding>
                                        <!--Bind input_value.Text-->
                                    </Binding>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

enter image description here


Solution

  • You can directly bind CompplexProperty.PartA and PartB also you can use ElementName method, In the below code you can see the both ways also.

                      <TextBlock >
                                <TextBlock.Text>
                                    <MultiBinding Converter="{StaticResource join_converter}">
                                        <Binding ElementName="ATextblock" Path="Text">                                       
                                        </Binding>
                                        <Binding Path="ComplexProperty.PartB">                                      
                                        </Binding>
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>