次のスタイルのサーフェスリストボックスがあります。
<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=HorizontalContentAlignment; DataItem=null; ターゲット要素は 'SurfaceListBoxItem' (Name='') です。ターゲット プロパティは 'HorizontalContentAlignment' (タイプ 'HorizontalAlignment') です
コードIsSelected
を介して設定せずにアイテムをクリックすると、すべて問題ありません。true
何か案は?