将usercontrol属性绑定到自定义类 [英] Binding usercontrol property to custom class

查看:92
本文介绍了将usercontrol属性绑定到自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在主窗口上使用一个名为 ChannelControls的 usercontrol 实例,它实例执行了6次。

In my application I'm using a usercontrol called "ChannelControls" which I instance 6 times, on the mainwindow.

public partial class ChannelControls : UserControl
{
    CMiXData cmixdata = CMiXData.Instance;
    public ChannelControls()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public static readonly DependencyProperty ChannelSpriteCountProperty =
    DependencyProperty.Register("ChannelSpriteCount", typeof(string), typeof(ChannelControls), new PropertyMetadata("1"));
    [Bindable(true)]
    public string ChannelSpriteCount
    {
        get { return (string)this.GetValue(ChannelSpriteCountProperty); }
        set { this.SetValue(ChannelSpriteCountProperty, value); }
    }

我正在使用名为cmixdata的自定义类来保存所有数据对于我的应用程序(它将包含具有 List 字符串,双精度等的不同属性)。 ChannelControls 将包含许多滑块,按钮和其他用户控件,但目前我仅尝试绑定其中之一。

I'm making using a custom class called cmixdata to hold all the data for my application (it will contains different properties with List of string, double etc...). The ChannelControls will contains many sliders, button and other usercontrols but at the moment I'm trying to bind just one of them.

这里是此自定义类的一部分,它将保存数据,它有一个私有构造函数,因为我需要从任何地方访问它:

Here is one part of this custom class that will hold the data, it has a private constructor as I need to access it from anywhere :

[Serializable]
public class CMiXData : INotifyPropertyChanged
{
    private static CMiXData _instance = null;
    public static CMiXData Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new CMiXData();
            }
            return _instance;
        }
    }
    private CMiXData() { } //prevent instantiation from outside the class


    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
        MessageBox.Show(propertyName);
    }


    private List<string> _SpriteCount = new List<string>(new string[] {"1", "1", "1", "1", "1", "1"});
    public List<string> SpriteCount
    {
        get { return _SpriteCount; }
        set
        {
            if(_SpriteCount != value)
            {
                _SpriteCount = value;
                RaisePropertyChanged("SpriteCount");
            }
        }
    }

这就是我的方法m试图将channelcontrol属性 ChannelSpriteCount 绑定到我的单例类:cmixdata。

And here is how I'm trying to bind the channelcontrol property ChannelSpriteCount to my singleton class : cmixdata.

<CMiX:ChannelControls x:Name="Layer0" Tag="0" Visibility="Visible" ChannelSpriteCount="{Binding SpriteCount[0], Mode=TwoWay}"/>

在主用户控件上, ChannelControls 是例如, datacontext 是这样设置的:

On the main usercontrol, which ChannelControls is instanced, the datacontext is set this way :

public partial class CMiX_UI : UserControl
{
    BeatSystem beatsystem = new BeatSystem();
    CMiXData cmixdata = CMiXData.Instance;

    public CMiX_UI()
    {
        InitializeComponent();
        this.DataContext = cmixdata;
    }

在xaml方面:

<UserControl
        x:Class="CMiX.CMiX_UI"
        DataContext="{x:Static CMiX:CMiXData.Instance}"

但是由于某些原因,cmixdata中的属性未更新,并且始终具有默认值...

But for some reason the property in cmixdata is not updated and always has the default value...

推荐答案

UserControl永远不应具有视图模型的自己实例。相反,它应该具有绑定到外部视图模型的属性的依赖项属性。

A UserControl should never have an "own" instance of a view model. Instead, it should have dependency properties that are bound to properties of an "external" view model.

您的ChannelsControl会声明一个这样的属性(我假设 string 不是 count 的合适类型):

Your ChannelsControl would declare a property like this (where I suppose that string is not an appropriate type for a count):

public partial class ChannelsControl : UserControl
{
    public static readonly DependencyProperty SpriteCountProperty =
        DependencyProperty.Register(
            nameof(SpriteCount), typeof(string), typeof(ChannelsControl));

    public string SpriteCount
    {
        get { return (string)GetValue(SpriteCountProperty); }
        set { SetValue(SpriteCountProperty, value); }
    }

    ...
}

在ChannelsControl的XAML中,您可以这样绑定它:

In ChannelsControl's XAML, you would bind it like this:

<CMiX:Counter Count="{Binding SpriteCount,
                      RelativeSource={RelativeSource AncestorType=UserControl}}"/>

您现在将使用如下所示的UserControl,将Count属性绑定到视图模型中像这样的DataContext:

You would now use your UserControl like shown below, where you bind the Count property to a view model in the DataContext like this:

<Window.DataContext>
    <local:CMiXData />
</Window.DataContext>
...

<local:ChannelsControl SpriteCount="{Binding SpriteCount[0]}" ... />






您现在也可以在以下项的ItemTemplate中使用ChannelsControl一个这样的ItemsControl:


You may now also use ChannelsControl in the ItemTemplate of an ItemsControl like this:

<ItemsControl ItemsSource="{Binding SpriteCount}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:ChannelsControl SpriteCount="{Binding}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>






编辑:将Window的DataContext设置为您的视图模型这样的单例实例:


Set the Window's DataContext to your view model singleton instance like this:

<Window ... DataContext="{x:Static local:CMiXData.Instance}" >

或者在后面的代码中,在MainWindow构造函数中:

Or in code behind, in the MainWindow constructor:

DataContext = CMiXData.Instance;

这篇关于将usercontrol属性绑定到自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆