1

次のスタイルのサーフェスリストボックスがあります。

<s:SurfaceListBox.ItemContainerStyle>
    <Style TargetType="s:SurfaceListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="FontSize" Value="13" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
        <Setter Property="MinHeight" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type s:SurfaceListBoxItem}">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="#FF7E0123" />
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <ScaleTransform ScaleX="1.25" ScaleY="1.25"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</s:SurfaceListBox.ItemContainerStyle>

コードを使用してこのサーフェスリストボックスにデータを入力し、「すべて表示」項目を追加しています。

private void AddShowAllCategories(int totalItems)
{
    SurfaceListBoxItem CategoriesListBoxItem = new SurfaceListBoxItem();
    CategoriesListBox.Items.Insert(0, CategoriesListBoxItem);
    CategoriesListBoxItem.Content = "Show All " + "(" + totalItems + ")";
    CategoriesListBoxItem.Margin = new Thickness(0,30,0,0);
    CategoriesListBoxItem.IsSelected = true;  //This is what is causing the issue
}

コードIsSelectedを介してに設定するまで、すべてが正常に機能し、エラーは発生しません。true次に、次のエラーが表示されます。

System.Windows.Data エラー: 4 : 参照 'RelativeSource FindAncestor、AncestorType='System.Windows.Controls.ItemsControl'、AncestorLevel='1'' でバインディングのソースが見つかりません。BindingExpression:Path=Horizo​​ntalContentAlignment; DataItem=null; ターゲット要素は 'SurfaceListBoxItem' (Name='') です。ターゲット プロパティは 'Horizo​​ntalContentAlignment' (タイプ 'Horizo​​ntalAlignment') です

コードIsSelectedを介して設定せずにアイテムをクリックすると、すべて問題ありません。true何か案は?

4

1 に答える 1

0

コードを使用してアイテムを選択するコード ビハインドを提供していないため、MVVM を使用しているか、コード ビハインドを使用しているか、または以下の方法を既に試したかどうかはわかりません。

私はあなたのシナリオに従い、「ItemContainerGenerator」を使用してコードからアイテムを選択することができました:

        // Selecting an item given the actual item
        object requestedItem = null; // TODO - You should set here the actual item
        SurfaceListBoxItem itemContainer = itemsList.ItemContainerGenerator.ContainerFromItem(requestedItem) as SurfaceListBoxItem;
        itemContainer.IsSelected = true;

        // Selecting an item given his index in the item source
        SurfaceListBoxItem itemContainer2 = itemsList.ItemContainerGenerator.ContainerFromIndex(2) as SurfaceListBoxItem;
        itemContainer2.IsSelected = true;

選択するアイテムへの参照がある場合は最初の方法を使用し、提供されたリストのインデックスしか知らない場合は 2 番目の方法を使用します。

うまくいったかどうか教えてください。

エラン。

于 2012-08-21T05:19:23.623 に答える