如何在DataTemplate中绑定模板元素属性

时间:2019-05-06 05:42:56

标签: c# wpf data-binding bind

我想让模板元素属性创建与另一个元素属性的绑定,但是我发现了很多文章,但都没有谈论。所以我问我该怎么办。

<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

1 个答案:

答案 0 :(得分:2)

您可以直接绑定CompplexProperty.PartA和PartB,也可以使用ElementName方法,在 在下面的代码中,您也可以同时看到两种方式。

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