C# 如何在DataTemplate中绑定模板元素属性
我想让一个模板元素属性创建一个与另一个元素属性的绑定,但是我找到了很多文章,其中没有一篇讨论过。所以我问我该怎么做C# 如何在DataTemplate中绑定模板元素属性,c#,wpf,data-binding,bind,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
<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>
您可以直接绑定CompplexProperty.PartA和PartB,也可以在 在下面的代码中,您也可以看到这两种方法
<TextBlock >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource join_converter}">
<Binding ElementName="ATextblock" Path="Text">
</Binding>
<Binding Path="ComplexProperty.PartB">
</Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
您可以直接绑定CompplexProperty.PartA和PartB,也可以在 在下面的代码中,您也可以看到这两种方法
<TextBlock >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource join_converter}">
<Binding ElementName="ATextblock" Path="Text">
</Binding>
<Binding Path="ComplexProperty.PartB">
</Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Mmm,为什么不改为绑定到ComplexProperty.PartA
和ComplexProperty.PartB
呢?很抱歉,我举了一个不好的例子,让您感到困惑,我只是要求绑定元素的属性。例如,我想将Image.Source绑定到multibinding.Mmm,为什么不绑定到ComplexProperty.PartA
和ComplexProperty.PartB
呢?很抱歉,我举了一个不好的例子,让您感到困惑,我只是要求绑定元素的属性。例如,我想将Image.Source绑定到多重绑定中。很抱歉,我举了一个不好的例子,让您感到困惑,我只是要求用元素的属性绑定。例如,我想将Image.Source绑定到多重绑定中。@MikiraSora你能更新这个问题吗,不能理解你需要绑定更新的内容,谢谢你花时间在我愚蠢的问题上:DSorry我举了一个坏例子,让你感到困惑,我只是要求用元素的属性绑定。例如,我想将Image.Source绑定到多重绑定中。@MikiraSora你能更新这个问题吗,不能理解你需要什么绑定更新,谢谢你花时间在我愚蠢的问题上:D