我可以将这种绑定变成一种风格吗?

Ber*_*ryl 1 wpf resources styles resourcedictionary

我想把我目前拥有的xaml用于ComboBox(下面),并将其浓缩成类似Style的内容,如下所示.我认为这应该有效,但我有一个'类型'问题,不知道如何解决它

"Cannot resolve the Style Property 'Margin'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.)
Run Code Online (Sandbox Code Playgroud)

当我看到现有的ComboBoxStyle(也在下面)我希望将这种新风格作为基础时,我看到我没有使用x:Type,但它似乎确实有效.

这种新风格应该不起作用吗?我该改变什么?

干杯,
Berryl

组合框,工作):

<ComboBox 
    x:Name="cboDepartmentFilter" Style="{StaticResource ComboBoxStyle}"
    Margin="{StaticResource FliterPanelItem_Margin}" Width="{StaticResource FilterPanelItem_Width}"
    ItemsSource="{Binding Path=DepartmentFilterControl.Choices}"                     
    ToolTip="{Binding DepartmentFilterControlData.ToolTipTitle}"                    
    />
Run Code Online (Sandbox Code Playgroud)

我想要的是:

<ComboBox Style="{StaticResource FilterPanelComboBoxStyle}" DataContext="{Binding DepartmentFilterControl}"  />

<!- in some resource file ->
<Style x:Key="FilterPanelComboBoxStyle" BasedOn="{StaticResource ComboBoxStyle}">
    <Setter Property="Margin" Value="{StaticResource FliterPanelItem_Margin}" />
    <Setter Property="Width" Value="{StaticResource FilterPanelItem_Width}" />
    <Setter Property="ItemsSource" Value="{Binding Choices}" />
    <Setter Property="ToolTip" Value="{Binding ToolTipTitle}" />
</Style>

<!-- 
Run Code Online (Sandbox Code Playgroud)

此样式定义筛选器面板中项目的公共边距. - > 150

现有的ComboBoxStyle:

<!-- ComboBox Style -->
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
    <Setter Property="Background" Value="{StaticResource headerBrush}" />
    ...
    <Setter Property="Template">
        <Setter.Value>
        ...
        </Setter.Value>
    </Setter>

    <Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}" />
    <Setter Property="IsSynchronizedWithCurrentItem" Value="True" />

</Style>
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 5

您仍需要TargetType在派生样式中指定.(或者您为属性添加前缀"ComboBox.")