【问题标题】:Change Binding of ContentControl Based on Selection in one of two ListBoxes根据两个 ListBox 之一中的选择更改 ContentControl 的绑定
【发布时间】:2011-03-23 07:02:42
【问题描述】:

我在一个窗口中有两个 ListBox 控件。每个都是绑定到不同静态资源的患者过敏列表。在窗口底部,我有一个 ContentControl,它显示有关上面列表中所选过敏的其他信息。

目前,我有两个 ContentControl,每个列表框一个,都带有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"));

在每种情况下,我都尝试仅使用一个名为 ExpandedAllergyDetails 的 ContentControl,并将其绑定更改为“InsideAllergiesSource”,这是在 XAML 中定义的 CollectionViewSource。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    SetResourceReference 需要一个键,而不是实际的对象。所以你会使用

    this.ExpandedAllergyDetails.SetResourceReference(ContentControl.ContentProperty, "InsideAllergiesSource");
    

    如果“InsideAllergiesSource”是资源的 x:Key。我不确定这是否会绑定到“当前”项目。

    【讨论】:

    • 像魅力一样工作。那一定是我没有尝试过的唯一组合!谢谢!
    【解决方案2】:

    You can wrap those two lists in a ListBox as items, so that when one allergy is selected the corresponding main list is selected as well, that way you can bind to a path of selections, i.e. SelectedMainList -> SelectedAllergy.不确定这如何转换为您的特定应用程序代码,但这是该场景的一个工作示例,请注意自动选择父列表所需的样式。该示例由两个列表和一个显示所选项目的 TextBlock 组成。

    <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>
    

    您可能想要屏蔽两个主要列表的选择,this answer 应该会有所帮助。


    编辑:由于上述样式的触发器也会在键盘焦点从列表内部移开时取消选择主列表,您可能希望将其更改为仅在触发触发器时才起作用,这可以通过触发器的EnterActions 中的单帧动画来完成。

    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                                <DiscreteBooleanKeyFrame KeyTime="0:0:0.0" Value="True"/>
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多