参考父控件的类型设置 DataTrigger 的样式

发布于 2024-10-05 01:25:05 字数 1072 浏览 3 评论 0原文

在我的窗口上有几个 GroupBox 控件,每个控件都包含一个网格控件。我想为这些网格指定一个样式。但仅限于那些直接位于 GroupBox 中的网格,所有其他网格不应受到影响。

我尝试了以下方法,但它不起作用,因为 GetType() 不是属性。

<Style TargetType="Grid">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.GetType()}" Value="{x:Type GroupBox}">
           <!-- <Setter Property="..." Value="..."/> -->
        </DataTrigger>
    </Style.Triggers>
</Style>

我找到了一种解决方法,但这并不是一个真正完美的解决方案,因为我必须修改 GroupBoxes:

<Style TargetType="GroupBox">
    <Setter Property="Tag" Value="blub"/>
 </Style>
<Style TargetType="Grid">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub">
           <!-- <Setter Property="..." Value="..."/> -->
        </DataTrigger>
    </Style.Triggers>
</Style>

显然我可以手动设置每个网格的样式,但我试图避免这种情况,因为它们有很多。我希望您能找到一种方法使第一个示例发挥作用。

On my Window there are several GroupBox Controls, each containing a Grid Control. To those Grids I want to asign a Style. But only to those Grids that are directly in a GroupBox, all other Grids should not be affected.

I have tried the following, which does not work as GetType() is no property.

<Style TargetType="Grid">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.GetType()}" Value="{x:Type GroupBox}">
           <!-- <Setter Property="..." Value="..."/> -->
        </DataTrigger>
    </Style.Triggers>
</Style>

I have found a workaround, but it's not really a beautiful solution, as I have to modify the GroupBoxes:

<Style TargetType="GroupBox">
    <Setter Property="Tag" Value="blub"/>
 </Style>
<Style TargetType="Grid">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub">
           <!-- <Setter Property="..." Value="..."/> -->
        </DataTrigger>
    </Style.Triggers>
</Style>

Obviously I could set the style for each Grid manually, but I'm trying to avoid that, as there are quite a lot of them. I hope you can find a way to make the first example work.

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

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

发布评论

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

评论(3

冷…雨湿花 2024-10-12 01:25:05
<DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub">

这段代码不起作用,因为Mode的类型实际上是BindingMode,它是一个Enumeration,并且它的成员都不是Self。所以这个赋值 Mode=Self 在你的代码中是错误的。要了解模式的可能值,单击此

正确的编写方法是,

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="blub">

当然,为了使其工作,您必须保留已经编写的 GroupBox 样式。

<DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub">

This code would not work, because type of Mode is actually BindingMode which is an Enumeration, and none of it's member is Self. So this assignment Mode=Self is wrong in your code. To know the possible values of Mode, click this.

The correct way to write this is,

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="blub">

And of course, for this to work, you've to keep that Style for GroupBox which you've already written.

爱格式化 2024-10-12 01:25:05

这对我有用:

        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StatusBar}}, Path=DependencyObjectType.Name}" Value="StatusBar">
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Background" Value="Chartreuse"/>
            </DataTrigger>
        </Style.Triggers>

它允许您根据父类型设置样式,而不必求助于标签,标签实际上应该由代码而不是标记使用。

This worked for me:

        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StatusBar}}, Path=DependencyObjectType.Name}" Value="StatusBar">
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Background" Value="Chartreuse"/>
            </DataTrigger>
        </Style.Triggers>

It allows you to set style based on parent type without having to resort to Tag which should really be used by code rather than by markup.

独闯女儿国 2024-10-12 01:25:05

使用以下代码:

using DevExpress.Xpf.Core.Native;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace BindingErrorHelper
{
    public class IsTypeFoundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            FrameworkElement element = value as FrameworkElement;
            Type type = parameter as Type;
            if (element != null && type != null)
            {
                element = LayoutHelper.FindElement(element,type);
                if (element != null)
                    return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class LayoutHelper
    {
        public static FrameworkElement FindElement(FrameworkElement treeRoot, Type type)
        {
        FrameworkElement parentElement = VisualTreeHelper.GetParent(treeRoot) as FrameworkElement;
        while (parentElement != null)
        {
            if (parentElement.GetType() == type)
                return parentElement;
            else
                parentElement = VisualTreeHelper.GetParent(parentElement) as FrameworkElement;
        }
        return null;
        }
    }
}

将 XAML 代码编写为:

<tt:IsTypeFoundConverter x:Key="isTypeFoundConverter"/>

<Style TargetType="Grid">
    <Style.Triggers>
        <DataTrigger Binding={Binding RelativeSource={RelativeSource Self}, Converter={StaticResource isTypeFoundConverter}, ConverterParameter={x:Type GroupBox}}" Value="true">
           <!-- <Setter Property="..." Value="..."/> -->
        </DataTrigger>
    </Style.Triggers>
</Style>

Use following Code :

using DevExpress.Xpf.Core.Native;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace BindingErrorHelper
{
    public class IsTypeFoundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            FrameworkElement element = value as FrameworkElement;
            Type type = parameter as Type;
            if (element != null && type != null)
            {
                element = LayoutHelper.FindElement(element,type);
                if (element != null)
                    return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class LayoutHelper
    {
        public static FrameworkElement FindElement(FrameworkElement treeRoot, Type type)
        {
        FrameworkElement parentElement = VisualTreeHelper.GetParent(treeRoot) as FrameworkElement;
        while (parentElement != null)
        {
            if (parentElement.GetType() == type)
                return parentElement;
            else
                parentElement = VisualTreeHelper.GetParent(parentElement) as FrameworkElement;
        }
        return null;
        }
    }
}

Write the XAML Code as :

<tt:IsTypeFoundConverter x:Key="isTypeFoundConverter"/>

<Style TargetType="Grid">
    <Style.Triggers>
        <DataTrigger Binding={Binding RelativeSource={RelativeSource Self}, Converter={StaticResource isTypeFoundConverter}, ConverterParameter={x:Type GroupBox}}" Value="true">
           <!-- <Setter Property="..." Value="..."/> -->
        </DataTrigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文