0

この質問のようなエキスパンダーのリストボックスを作成しました: Expander Auto Open/Close

リストボックスがウィンドウ上の唯一のアイテムである場合、ソリューションはエキスパンダーのコンテンツで機能します。

しかし、カスタム コントロールで同じコードを使用しようとすると、次のエラーが発生します。

System.Windows.Data エラー: 4: 参照 'RelativeSource FindAncestor、AncestorType='System.Windows.Controls.ListBoxItem'、AncestorLevel='1'' を使用してバインディングのソースが見つかりません。BindingExpression:Path=IsSelected; DataItem=null; ターゲット要素は 'Expander' (Name='') です。ターゲット プロパティは 'IsExpanded' (タイプ 'Boolean') です

あるポスターが別のスレッドで提案したように、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>

惨めに失敗!!!!!

どんな助けでも感謝します:)

4

1 に答える 1

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

私にとってはうまくいくようです。リスト項目をクリックすると展開され、ボタンを使用して、選択したインデックスを展開して設定することで具体的に設定することもできます。非常に怪しい何かがあなたの特定のシナリオに影響を与えています... :]

于 2013-01-11T00:57:12.737 に答える