扩展器的ListBox不像radiobuttons

时间:2013-01-10 17:45:39

标签: wpf radio-button expander

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

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

但是当我尝试在自定义控件中使用相同的代码时,我收到此错误:

  

System.Windows.Data错误:4:找不到绑定源   参考'RelativeSource FindAncestor,   AncestorType = 'System.Windows.Controls.ListBoxItem',   AncestorLevel = '1'”。 BindingExpression:路径= IsSelected;的DataItem = NULL;   target元素是'Expander'(Name ='');目标属性是   '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>

悲惨地失败!!!!!

任何帮助表示赞赏:)

1 个答案:

答案 0 :(得分:2)

这对我来说是一个很大的场景,但是我想到了一些尝试:

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

似乎对我来说很好。单击列表项会展开,甚至使用按钮通过设置扩展的选定索引来专门设置它。非常可疑的东西会影响您的具体情况......:]