我可以将此绑定转化为样式吗?

发布于 2024-11-16 03:58:22 字数 2045 浏览 7 评论 0原文

我想采用当前 ComboBox 的 xaml(如下),并将其压缩为类似下面所示的样式。我认为这应该可行,但我有一个“打字”问题,不确定如何解决它

"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,但它似乎确实有效。

这种新风格有什么理由不适用吗?我必须改变什么?

干杯,
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}"                    
    />

我想要的:

<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

现有的 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>

I would like to take the xaml I currently have for a ComboBox (below), and condense it into something like the Style also shown below. I think this should work, but I have a 'Type'ing issue and not sure how to resolve it

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

As I look at the existing ComboBoxStyle (also below) that I'm looking to base this new style off of, I see that I hadn't used x:Type, but it does seem to work.

Is there any reason this new style shouldn't work? What must I change?

Cheers,
Berryl

combo box, as is, working):

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

what I want:

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

<!-- 

This style defines a common margin for items in a filter panel.
-->

150

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2024-11-23 03:58:22

您仍然需要在派生样式中指定 TargetType。 (或者您可以在属性前加上“ComboBox。”

You still need to specify the TargetType in the derived style. (Or you prefix the properties with "ComboBox.")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文