XAML의 의미에 대해 혼란스러워합니다. XAML에 익숙해지기 전까지는 C#에 상응하는 것으로 생각하는 것이 도움이 될 수 있습니다. 이것은 당신이 지금 무슨 일을하는지 본질적 :
Style BorderStyle = new Style();
Border inlineStyle = new Border { Style = BorderStyle };
Style listBoxItemDefaultStyle = new Style();
listBoxItemDefaultStyle.Setters.Add(new Setter(StyleProperty, inlineStyle));
ListBoxItem item = new ListBoxItem { Style = listBoxItemDefaultStyle };
하나의 문제는 당신이 어떤 종류의 원인이가는 물론 당신의 ListBoxItem의의 스타일 내부 세터,에서 ListBoxItem의 스타일을 설정하는 것입니다 재귀 문제. 그래서 우리가 얻을 코드에서 여분의 스타일을 제거 : 국경 개체 (유형 스타일의) 스타일 속성을 설정하려고 있기 때문에
Style BorderStyle = new Style();
Border inlineStyle = new Border { Style = BorderStyle };
ListBoxItem item = new ListBoxItem { Style = inlineStyle };
이 유효하지 않습니다. 이것은 본질적으로 당신이보고있는 오류의 핵심입니다.
이 경우 정말로 원하는 것은 테두리 스타일을 통합하기 위해 ListBoxItem ControlTemplate을 변경하는 것입니다. 이것은 TemplateBindings를 사용하여 속성을 설정하는 표준 템플릿 대신 테두리를 사용하도록 수정 된 기본 스타일입니다.
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" Style="{StaticResource BorderStyle}" Width="200">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
무엇을 하시겠습니까? 더 설명해 줄 수 있어요! – Rev
나는 listBoxItem에 대한 내 자신의 스타일을 만들고 싶습니다. ListBox의 모든 Item에 위의 테두리와 같은 스타일이 있어야합니다. – Polaris