Wpf 如何设置依赖项值
我有一些控制能力,比如Wpf 如何设置依赖项值,wpf,Wpf,我有一些控制能力,比如 <ListBox ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource TestTemplate}" /> 当用户点击ListBox的某些元素时,我需要在其他控件中获取此ListBoxItem元素的数据元素,并使用其他元素的数据模板传播它。如
<ListBox ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource TestTemplate}" />
当用户点击ListBox的某些元素时,我需要在其他控件中获取此ListBoxItem元素的数据元素,并使用其他元素的数据模板传播它。如何正确处理?例如:
资料来源:
<ListBox ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource TestTemplate}" />
目标:
<TextBlock Text="{Binding Path=name}" />
其中文本框中的文本绑定到选定列表框项的同一数据元素上
更新:
如何使某些控件的内容绑定到SelectedItem并由静态数据模板描述,如下所示:
<DataTemplate x:Key="TestTemplate">
<TextBlock Text="{Binding Path=name}"/>
</DataTemplate>
解决方法:
<ContentPresenter
HorizontalAlignment="Stretch"
Content="{Binding ElementName=tests_flat, Path=SelectedItem}"
ContentTemplate="{StaticResource TestInfoTemplate}">
</ContentPresenter>
您可以直接绑定到属性(如果项目是字符串),也可以设置列表框的名称并绑定到属性。来源
<ListBox x:Name="myListBox" ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource TestTemplate}" />
目标
<TextBlock Text="{Binding ElementName=myListBox path=SelectedItem}" />
ok,还有一个答案:如何将数据模板作为静态资源绑定到此绑定?