Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
C# 使用视图模型的依赖项属性更改矩形的填充_C#_Wpf_Xaml_Mvvm_Dependency Properties - Fatal编程技术网

C# 使用视图模型的依赖项属性更改矩形的填充

C# 使用视图模型的依赖项属性更改矩形的填充,c#,wpf,xaml,mvvm,dependency-properties,C#,Wpf,Xaml,Mvvm,Dependency Properties,我有一个矩形视图,根据一个布尔变量,我想将填充设置为一个特定的笔刷。为此,我在ViewModel中添加了DependencyProperty public static readonly DependencyProperty FalseColorProperty = DependencyProperty.Register( "FalseColor", typeof(Brush), typeof(BooleanRectangl

我有一个矩形视图,根据一个布尔变量,我想将填充设置为一个特定的笔刷。为此,我在ViewModel中添加了DependencyProperty

public static readonly DependencyProperty FalseColorProperty = DependencyProperty.Register(
            "FalseColor",
            typeof(Brush),
            typeof(BooleanRectangleView),
            new FrameworkPropertyMetadata(Application.Current.FindResource("LightGreyBrush"), FrameworkPropertyMetadataOptions.AffectsRender));

public Brush FalseColor
{
    get { return (Brush)GetValue(FalseColorProperty); }
    set { SetValue(FalseColorProperty, value); }
}
在视图中,我使用以下触发器向矩形添加了一个样式

<DataTrigger Binding="{Binding DataContext.Model.Variable, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="false">
                        <Setter Property="Fill" Value="{Binding DataContext.FalseColor, RelativeSource={RelativeSource AncestorType=UserControl}}" />
</DataTrigger>

因为我在ViewModel中有DependencyProperty,所以我不知道在创建UserControl时如何设置它。例如,以下操作不起作用

<BooleanRectangleView FalseColor="..."/>

此外,在运行程序时,ViewModel的构造函数中出现异常:

默认类型与属性的类型不匹配
FalseColor
(翻译自德语)

编辑:

当我将FindResource强制转换为Brush时,我得到一个新的异常:

属性
FalseColor
的默认值不能绑定到特定线程


我猜这与FindResource有关,不一定是从dispatcher线程调用的?

我不理解您的结构:您告诉我FalseColorProperty在ViewModel层中,但该类称为BooleanRectangleView,它可能是视图类(UserControl或类似的名称)的名称

在您的示例中,除了名为IsVariableTrue的bool之外,还将在视图中显示此DP

在ViewModel中,您将拥有Variable属性,该属性使模型中的Variable属性可访问

在BooleanRectangleView XAMl中,您将拥有一种样式:

<Rectangle>
    <Rectangle.Style>
        <Style TargetType="{x:Type Rectangle}">
            <Setter Property="Fill" Value="{Binding RelativeSource={RelativeSource Self}, Path=Fill}"/> <!-- Binding to the Rectangle Fill property -->
            <Style.Triggers>
                <DataTrigger Property="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type BooleanRectangleView}}, Path=IsVariableTrue}" Value="False">
                    <Setter Property="Fill" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type BooleanRectangleView}}, Path=FalseColor}"/> <!-- Binding to the DP in the View -->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
    ...
</Rectangle>
您需要在BooleanRectangleView的构造函数中设置DP值:

<BooleanRectangleView IsVariableTrue="{Binding Variable}"/> <!-- Binding to the ViewModel -->
public BooleanRectangleView() {

    InitializeComponents();
    FalseColor = (Brush)Application.Current.FindResource("Infoteam_LightGreyBrush");
}
我相信有一个更好的解决方案,但它应该可以很好地工作

编辑

将TemplateBinding替换为使用RelativeSource的绑定,因为我们不在ControlTemplate中。
将触发器替换为DataTrigger,因为IsVariableTrue不存在于矩形中。

如果在ViewModel类中放置DP,则此类将成为DependencyObject。你不会想要的。您只需要一个引发INotifyPropertyChanged的属性。你可能写错了,它可能在你的视图类中。关于您的问题:您需要在样式和DataTrigger中设置FalseColor。关于默认值问题:您需要将FindResource强制转换为Brush@nkoniishvt:您能解释一下为什么我不希望ViewModel成为DependencyObject吗?拥有dependency属性(通常)的任何原因都不适用于视图模型属性。您不希望视图模型特性成为绑定的目标,或在样式设置器中设置,或设置动画等。ViewModel层应该为视图提供要显示的数据,并允许视图层更新模型层中的数据。作为一个DependencyObject意味着它是一个视图元素,并且有很多开销,这对于ViewModel层是没有用的(例如,Dispatcher、属性动画,对于ViewModel类是没有用的)@nkonishvt:好的,我明白了,但是我有一个问题,我有两个数据上下文。一个用于ViewModel,一个用于DependencyProperty。这是怎么回事?为了正确起见,我应该把DP声明放在哪里?在视图中?我得到错误
FalseColor成员无效,因为它没有限定的类型名称
,并且
成员FalseColor无法识别或无法访问
on
Value=“{TemplateBinding FalseColor}”
@gartenriese和yes,DP声明应该在视图的代码后面编辑一些不起作用的东西(没有测试代码)
public BooleanRectangleView() {

    InitializeComponents();
    FalseColor = (Brush)Application.Current.FindResource("Infoteam_LightGreyBrush");
}