简体   繁体   English

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

[英]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 在此处输入图片说明

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. 您可以直接绑定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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM