【问题标题】:ListBox of expanders not acting like radiobuttons扩展器列表框不像单选按钮
【发布时间】:2012-12-25 04:52:14
【问题描述】:

我已经创建了一个类似这个问题的扩展器列表框:Expander Auto Open/Close

当列表框是窗口上的唯一项目时,该解决方案适用于扩展器中的内容。

但是当我尝试对我的自定义控件使用相同的代码时,我收到了这个错误:

System.Windows.Data 错误:4:找不到绑定源 参考'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListBoxItem', 祖先级别='1''。绑定表达式:路径=IsSelected;数据项=空; 目标元素是'扩展器'(名称='');目标属性是 'IsExpanded'(类型'布尔')

我已尝试将 ListBox.ItemContainerStyle 中的 IsSelected 属性添加为另一个帖子中建议的海报,但失败了。

<ListBox Margin="5"
         SelectionMode="Single"
         ScrollViewer.VerticalScrollBarVisibility="Auto">
    <ListBox.Resources>
        <Style TargetType="{x:Type Expander}">
             <Setter Property="IsExpanded"
                     Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
        </Style>
        <Style TargetType="{x:Type controls:SelectionCriteriaControl}">
             <Setter Property="MaxHeight"
                     Value="200" />
        </Style>
    </ListBox.Resources>
    <Expander Header="Fund Family" Margin="2">
        <StackPanel>
            <controls:SelectionCriteriaControl DataContext="{Binding FundFamilySelectionCriteria, Mode=TwoWay}" />
        </StackPanel>
    </Expander>
    <Expander Header="Asset Class" Margin="2">
        <StackPanel>
            <controls:SelectionCriteriaControl DataContext="{Binding AssetClassSelectionCriteria, Mode=TwoWay}" />
        </StackPanel>
    </Expander>
    <ListBox.Template>
        <ControlTemplate TargetType="{x:Type ListBox}">
            <ItemsPresenter />
        </ControlTemplate>
    </ListBox.Template>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter Content="{TemplateBinding Content}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

惨败!!!!!!

任何帮助表示赞赏:)

【问题讨论】:

  • 它是怎么失败的?你在屏幕上看到了什么?
  • 扩展器应该是互斥的,就像分组的单选按钮一样。如果您单击一个,而另一个是打开的,则第一个应该关闭。如果您将 StackPanels 中嵌入的 UserControl 替换为 TextBlock(例如),它就可以工作。

标签: wpf radio-button expander


【解决方案1】:

目前我要设置的场景有点大,但我想到了一些尝试:

Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" />

我不认为在哪里使用模板可以按类型定义相对源。

编辑:此代码运行良好:根据您的原始代码,不需要 TemplatedParent。

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <ListBox x:Name="testList" Margin="5"
     SelectionMode="Single"
     ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListBox.Resources>
            <Style TargetType="{x:Type Expander}">
                <Setter Property="IsExpanded"
                 Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
            </Style>
        </ListBox.Resources>
        <Expander Header="Fund Family" Margin="2">
            <StackPanel>
                <TextBlock Text="First"/>
                <TextBlock Text="Second"/>
            </StackPanel>
        </Expander>
        <Expander Header="Asset Class" Margin="2">
            <StackPanel>
                <TextBlock Text="First"/>
                <TextBlock Text="Second"/>
            </StackPanel>
        </Expander>
        <ListBox.Template>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <ItemsPresenter />
            </ControlTemplate>
        </ListBox.Template>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <ContentPresenter Content="{TemplateBinding Content}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
    <Button HorizontalAlignment="Left" Content="Test" Grid.Row="1" Width="60" Click="Button_Click"/>
</Grid>

还有一个快速的代码隐藏来触发编程SelectedIndex 设置...

        private void Button_Click(object sender, RoutedEventArgs e)
    {
        testList.SelectedIndex = 1;
    }

对我来说似乎工作得很好。单击列表项展开,甚至使用按钮通过设置它展开的选定索引来专门设置它。一些非常可疑的东西正在影响您的特定场景...... :]

【讨论】:

  • 如果我将我的 UserControl 替换为 TextBox,是什么让我抓狂,模板按预期工作。有什么我不需要处理的事情吗?
  • 我玩过你的代码,我认为你在追逐一个吉普赛马车。我将您的代码粘贴到一个测试项目中,用几个文本块替换了您的用户控件,甚至添加了一个在列表框上设置SelectedIndex 的按钮,并且扩展器展开没有错误。我认为您还有其他问题。我已经使用添加的控件修改了答案。将其弹出窗口并尝试一下,以防出现某些特定的环境问题...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2013-11-03
  • 2016-04-05
  • 2013-07-17
  • 2012-12-14
  • 1970-01-01
相关资源
最近更新 更多