Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何设置依赖项值_Wpf - Fatal编程技术网

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,还有一个答案:如何将数据模板作为静态资源绑定到此绑定?