简体   繁体   English

如何在MVVM模式wpf中绑定StrokeDashArray属性

[英]How can bind StrokeDashArray property in MVVM pattern wpf

I have WPF application where I have in one canvas some lines to be need to be dotted line, and some is need to be normal line. 我有WPF应用程序,其中我在一个画布中有一些行需要是虚线,而某些则需要是普通行。 My question is what exactly type is StrokeDashArray . 我的问题是StrokeDashArray到底是什么类型。 In msdn I see is use DoubleCollection but is from System.Windows.Media. 在msdn中,我看到使用DoubleCollection,但来自System.Windows.Media。 How can make property for StrokeDashArray used good practice in MVVM pattern. 如何为StrokeDashArray设置属性在MVVM模式中使用了良好实践。

If you want to bind to the dash array then it's a DoubleCollection, so you do something like this: 如果要绑定到破折号数组,则它是DoubleCollection,因此您可以执行以下操作:

    private DoubleCollection _Dashes = new DoubleCollection { 1, 2 };
    public DoubleCollection Dashes
    {
        get { return this._Dashes;}
        set { this._Dashes = value; RaisePropertyChanged(); }
    }

And then in XAML: 然后在XAML中:

    <Line Stroke="Black" StrokeThickness="2" StrokeDashArray="{Binding Dashes}" X1="0" Y1="50" X2="100" Y2="50"/>

If you're creating a dynamic array of Canvas elements then you'll need to create an ItemsControl and set ItemsControl.Panel to be a Canvas. 如果要创建Canvas元素的动态数组,则需要创建ItemsControl并将ItemsControl.Panel设置为Canvas。 Then you'll usually use Item.Template to declare the line for each element along with a Style that chooses between your different line/dash types using Data Triggers. 然后,通常将使用Item.Template声明每个元素的行以及使用数据触发器在不同的行/破折号类型之间进行选择的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM