C# 根据两个列表框之一中的选择更改ContentControl的绑定
我在一个窗口中有两个列表框控件。每个都是绑定到不同资源的患者过敏症列表。在窗口的底部,我有一个ContentControl,它在上面的列表中显示关于所选过敏反应的附加信息 目前,我有两个ContentControls,每个列表框一个,都带有C# 根据两个列表框之一中的选择更改ContentControl的绑定,c#,wpf,C#,Wpf,我在一个窗口中有两个列表框控件。每个都是绑定到不同资源的患者过敏症列表。在窗口的底部,我有一个ContentControl,它在上面的列表中显示关于所选过敏反应的附加信息 目前,我有两个ContentControls,每个列表框一个,都带有Visiblity=“Collapsed”。当用户做出选择时,我会使关联的内容控件可见,并折叠另一个控件。我只希望有一个内容控件并更改其绑定 到目前为止,我已经尝试了以下每一项,但都没有成功: this.ExpandedAllergyDetails.Cont
Visiblity=“Collapsed”
。当用户做出选择时,我会使关联的内容控件可见,并折叠另一个控件。我只希望有一个内容控件并更改其绑定
到目前为止,我已经尝试了以下每一项,但都没有成功:
this.ExpandedAllergyDetails.Content = "InsideAllergiesSource";
this.ExpandedAllergyDetails.SetBinding(ContentControl.ContentProperty, "InsideAllergiesSource");
this.ExpandedAllergyDetails.SetBinding(ContentControl.ContentProperty, new Binding());
this.ExpandedAllergyDetails.SetResourceReference(ContentControl.ContentProperty, this.Resources["InsideAllergiesSource"]);
this.ExpandedAllergyDetails.SetResourceReference(ContentControl.ContentProperty, this.FindResource("InsideAllergiesSource"));
在每种情况下,我都试图只使用一个名为ExpandedAllerGydeals的ContentControl,并将其绑定更改为“InsideAllergiesSource”,这是XAML中定义的CollectionViewSource。SetResourceReference使用一个键,而不是实际的对象。所以你会用
this.ExpandedAllergyDetails.SetResourceReference(ContentControl.ContentProperty, "InsideAllergiesSource");
如果“InsideAllergiesSource”是资源的x:键。但我不确定这是否会绑定到“当前”项。SetResourceReference使用的是键,而不是实际的对象。所以你会用
this.ExpandedAllergyDetails.SetResourceReference(ContentControl.ContentProperty, "InsideAllergiesSource");
如果“InsideAllergiesSource”是资源的x:键。我不确定这是否会绑定到“当前”项目。您可以将这两个列表包装在
列表框中作为项目,这样当选择一个项目时,相应的主列表也会被选择,这样您就可以绑定到选择路径,即SelectedMainList->SelectedAllergy
。不确定如何将其转换为特定的应用程序代码,但这里是该场景的一个工作示例,请注意自动选择父列表所需的样式。该示例由两个列表和一个显示所选项目的文本块组成
<TextBlock Text="{Binding ElementName=TestLB,
Path=SelectedItem.Content.SelectedItem.Content}"/>
<ListBox Name="TestLB">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
<ListBox>
<ListBoxItem>List1-Item1</ListBoxItem>
<ListBoxItem>List1-Item2</ListBoxItem>
<ListBoxItem>List1-Item3</ListBoxItem>
</ListBox>
</ListBoxItem>
<ListBoxItem>
<ListBox>
<ListBoxItem>List2-Item1</ListBoxItem>
<ListBoxItem>List2-Item2</ListBoxItem>
<ListBoxItem>List2-Item3</ListBoxItem>
</ListBox>
</ListBoxItem>
</ListBox>
您可以将这两个列表作为项目包装在列表框中,这样当选择一个列表时,相应的主列表也会被选择,这样您就可以绑定到选择路径,即SelectedMainList->SelectedAllergy
。不确定如何将其转换为特定的应用程序代码,但这里是该场景的一个工作示例,请注意自动选择父列表所需的样式。该示例由两个列表和一个显示所选项目的文本块组成
<TextBlock Text="{Binding ElementName=TestLB,
Path=SelectedItem.Content.SelectedItem.Content}"/>
<ListBox Name="TestLB">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
<ListBox>
<ListBoxItem>List1-Item1</ListBoxItem>
<ListBoxItem>List1-Item2</ListBoxItem>
<ListBoxItem>List1-Item3</ListBoxItem>
</ListBox>
</ListBoxItem>
<ListBoxItem>
<ListBox>
<ListBoxItem>List2-Item1</ListBoxItem>
<ListBoxItem>List2-Item2</ListBoxItem>
<ListBoxItem>List2-Item3</ListBoxItem>
</ListBox>
</ListBoxItem>
</ListBox>
工作起来很有魅力。那一定是我唯一没有尝试的组合!谢谢工作起来很有魅力。那一定是我唯一没有尝试的组合!谢谢