Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 引用枚举依赖属性的DataTrigger_Wpf_Xaml_Triggers_Wpf Controls_Dependency Properties - Fatal编程技术网

Wpf 引用枚举依赖属性的DataTrigger

Wpf 引用枚举依赖属性的DataTrigger,wpf,xaml,triggers,wpf-controls,dependency-properties,Wpf,Xaml,Triggers,Wpf Controls,Dependency Properties,解决方案:请看宜兰答案 我目前正在处理一些CustomControls,这就是其中之一。根据DirectionProperty,我希望使用DataTrigger更改linearGradientBrush的方向。我真的不能让它工作,希望你的帮助 看起来DataTrigger实际上无法获取值或方向。提前谢谢 桑霍洛 编辑:这样做会出现错误: System.Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,AncestorType='CustomC

解决方案:请看宜兰答案

我目前正在处理一些CustomControls,这就是其中之一。根据DirectionProperty,我希望使用DataTrigger更改linearGradientBrush的方向。我真的不能让它工作,希望你的帮助

看起来DataTrigger实际上无法获取值或方向。提前谢谢 桑霍洛

编辑:这样做会出现错误:

System.Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,AncestorType='CustomControlLibrary.ColoredProgress',AncestorLevel='1'的绑定源。绑定表达式:路径=方向;DataItem=null;目标元素为“ColoredProgress”(名称=“”);目标属性为“NoTarget”(类型为“Object”)

C#

XAML


我还没有运行您的代码,但我想您的问题是DataTrigger中的值绑定没有正确设置为您期望的枚举值

试试这个:(注意新的值绑定)



我想它应该可以工作,但我没有检查代码的其余部分,所以请在这里随时更新您的进度。

请使用常规触发器:

                <ControlTemplate TargetType="{x:Type local:ColoredProgress}">
                ...

                <ControlTemplate.Triggers>
                    <Trigger Property="Direction" Value="Decrease">
                        <Setter TargetName="increase" Property="Height" Value="0"/>
                        <Setter TargetName="decrease" Property="Height" Value="*"/>
                    </Trigger>
                    <Trigger Property="Direction" Value="Increase">
                        <Setter TargetName="increase" Property="Height" Value="*"/>
                        <Setter TargetName="decrease" Property="Height" Value="0"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

...
据我所知,数据触发器转到DataContext检查值,因为已经将方向定义为控件的依赖属性,所以可以直接获取值。此外,您不能指向数据上下文,因为您的数据上下文中没有任何属性可以为您提供所需的值。这就是为什么会出现绑定表达式错误。 如果你需要更多的解释,请告诉我


注意。

我收到一个错误:CustomControlLibrary中没有colorDirection这个名称。如何引用它?因为
colorDirection
是一个嵌套的枚举,所以您需要使用包含类限定符(我想?
Value=“{x:Static local:ColoredProgress.colorDirection.reduce}”
好吧,尝试在相同的命名空间中在自定义控件之外声明枚举。由于枚举在控件中始终用作公共属性,因此更适合在单独的文件中声明它。访问嵌套元素的语法如下:
Value=“{x:Static local:ColoredProgress+colorDirection.reduce}”
@Ilan感谢您的快速回复!对我也有用。你的回答完全可以理解,再次感谢!
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControlLibrary">

    <Style TargetType="{x:Type local:ColoredProgress}">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ColoredProgress}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            RenderTransformOrigin="0.5, 0.5"
                            DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColoredProgress}}}">

                        <Grid x:Name="PART_Bar">
                            <Grid Background="Transparent" Panel.ZIndex="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Rectangle Fill="{TemplateBinding Background}" Height="{Binding Path=Progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            </Grid>

                            <Grid Panel.ZIndex="0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" x:Name="increase"/>
                                    <RowDefinition Height="0" x:Name="decrease"/>
                                </Grid.RowDefinitions>
                                <Rectangle Grid.Row="0">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0">
                                            <GradientStop Color="Yellow" Offset="0.0" />
                                            <GradientStop Color="Red" Offset="1.0" />
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <Rectangle Grid.Row="1">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                            <GradientStop Color="Yellow" Offset="0.0" />
                                            <GradientStop Color="Red" Offset="1.0" />
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                            </Grid>
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=Direction, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColoredProgress}}}" Value="colorDirection.Decrease">
                            <Setter TargetName="increase" Property="Height" Value="0"/>
                            <Setter TargetName="decrease" Property="Height" Value="*"/>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Direction, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColoredProgress}}}" 
                 Value="{x:Static local:colorDirection.Decrease}">
        <Setter TargetName="increase" Property="Height" Value="0"/>
        <Setter TargetName="decrease" Property="Height" Value="*"/>
    </DataTrigger>
</ControlTemplate.Triggers>
                <ControlTemplate TargetType="{x:Type local:ColoredProgress}">
                ...

                <ControlTemplate.Triggers>
                    <Trigger Property="Direction" Value="Decrease">
                        <Setter TargetName="increase" Property="Height" Value="0"/>
                        <Setter TargetName="decrease" Property="Height" Value="*"/>
                    </Trigger>
                    <Trigger Property="Direction" Value="Increase">
                        <Setter TargetName="increase" Property="Height" Value="*"/>
                        <Setter TargetName="decrease" Property="Height" Value="0"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>