【问题标题】:Can I get this binding into a style?我可以把这个绑定变成一种风格吗?
【发布时间】:2011-06-17 21:58:48
【问题描述】:

我想将我目前拥有的 xaml 用于 ComboBox(如下),并将其压缩为如下所示的样式。我认为这应该可以,但我有一个“类型”问题,不知道如何解决它

"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.)

当我查看现有的 ComboBoxStyle(也在下方)时,我希望以此为基础创建这种新样式,我发现我没有使用 x:Type,但它似乎确实有效。

这种新风格有什么不应该起作用的原因吗?我必须改变什么?

干杯,
浆果

组合框,按原样工作):

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

我想要什么:

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

<!-- 

此样式定义过滤器面板中项目的公共边距。 --> 150

现有的组合框样式:

<!-- 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>

【问题讨论】:

    标签: wpf resources styles resourcedictionary


    【解决方案1】:

    您仍然需要在派生样式中指定TargetType。 (或者你在属性前面加上"ComboBox."

    【讨论】:

    • 是的,很明显!我想通了,正要编辑这个,但你打败了我。干杯
    猜你喜欢
    • 2020-04-24
    • 2018-09-17
    • 2017-03-03
    • 1970-01-01
    • 2023-03-08
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多