如何将WPF控件绑定到字典中的对象的属性?

问题描述 投票:0回答:1

我是WPF的新手,并学习如何绑定控件。我创建了一个具有不同控件的UI,例如组合框和微调器(用户定义的控件)。现在我想将诸如组合框之类的控件绑定到对象的属性。下面是示例代码。

public class Parameter : INotifyPropertyChanged
{
    protected decimal m_Code;

    public decimal CODE
    {
        get { return m_Code; }
        set
        {
            if (m_Code != value)
            {
                m_Code = value;
                NotifyPropertyChanged("CODE");
            }
        }
    }

    protected decimal m_CurrentValue;

    public decimal CURRENT_VALUE
    {
        get { return m_CurrentValue; }
        set
        {
            if (m_CurrentValue != value)
            {
                m_CurrentValue = value;
                NotifyPropertyChanged("CURRENT_VALUE");
            }
        }
    }

    protected Dictionary<int, string> m_ItemsDict;

    public Dictionary<int, string> ItemsDict
    {
        get { return m_ItemsDict; }
        set
        {
            //if (m_dict != value) Not comparing dictionaries. We should set if we get this request
            {
                m_ItemsDict = value;
                NotifyPropertyChanged("ItemsDict");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

下面的类是一个单例,它有一个字典,其中Key是一个int,value是参数类的一个对象。

public class ParameterDB: INotifyPropertyChanged
{
    private static ParameterDB m_instance = new ParameterDB();

    public static ParameterDB Instance
    {
        get { return m_instance; }
    }

    private Dictionary<int, Parameter> m_ParamDict; 

    private ParameterDB()
    {
        m_ParamDict = new Dictionary<int, Parameter>();
        for(int i=0; i< 300; i++)
        {
            m_ParamDict.Add(i, new Parameter());
        }
    }

    public Dictionary<int, Parameter> ParamDict
    {
        get
        {
            return m_ParamDict;
        }

        set
        {
            m_ParamDict = value;
            NotifyPropertyChanged("ParamDict");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

现在我想绑定一个控件,比方说,ParamDict [0] .CURRENT_VALUE。我创建了一个自定义组合框类。

public class MyComboBox : System.Windows.Controls.ComboBox
{
    public readonly static DependencyProperty CodeProperty = DependencyProperty.Register(
        "Code",
        typeof(UInt32),
        typeof(FlexSIMComboBox),
        new PropertyMetadata(new UInt32()));

    public UInt32 Code
    {
        get { return (UInt32)GetValue(CodeProperty); }
        set
        {
            SetValue(CodeProperty, value);
        }
    }


    public readonly static DependencyProperty ValueProperty = DependencyProperty.Register(
        "Value",
        typeof(decimal),
        typeof(MyComboBox),
        new PropertyMetadata(new decimal(0), ValueChangedCallback));

    public decimal Value
    {
        get { return (decimal)GetValue(ValueProperty); }
        set
        {
            SetValue(ValueProperty, value);
        }
    }

    private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyComboBox myComboBox = d as MyComboBox;

        if (myComboBox != null && e.NewValue is decimal)
        {
            // Check if new value is under the limit
            decimal newValue = (decimal)e.NewValue;
            myComboBox.SelectedValue = (int)newValue;
        }
    }
}

XAML文件:

<local:MyComboBox 
    Grid.Column="1" x:Name="myCBCtrl" Margin="0,0,0,7"   Width="180"
    HorizontalAlignment="Left"  VerticalAlignment="Bottom" IsReadOnly="true" 
    Code="{Binding CODE, Mode =TwoWay}" 
    Value="{Binding CURRENT_VALUE, Mode =TwoWay}" 
    ItemsSource="{Binding ItemsDict, Mode =TwoWay, NotifyOnTargetUpdated=True}"
    SelectedValuePath="Key" DisplayMemberPath="Value" TargetUpdated="ComboBox_TargetUpdated"
    StaysOpenOnEdit="True" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"/>

我的问题是如何将此控件的DataContext设置为字典中的对象?例如ParamDict [CODE]。 CODE也是字典的关键。

我的目的是构建一个字典,然后将我的控件绑定到该字典中的项目属性。因此,如果项目的属性已更改,它也应反映在UI上。知道如何在XAML文件中执行此操作。

c# wpf dictionary binding datacontext
1个回答
0
投票

我想出了怎么做。

<local:MyComboBox DataContext="{Binding Source={x:Static local:ParameterDB.Instance}}"
    Grid.Column="1" x:Name="myCBCtrl" Margin="0,0,0,7"   Width="180"
    HorizontalAlignment="Left"  VerticalAlignment="Bottom" IsReadOnly="true" 
    Code="{Binding Path=ParamDict[0].CODE, Mode =TwoWay}" 
    Value="{Binding Path=ParamDict[0].CURRENT_VALUE, Mode =TwoWay}" 
    ItemsSource="{Binding Path=ParamDict[0].ItemsDict, Mode =TwoWay, NotifyOnTargetUpdated=True}"
    SelectedValuePath="Key" DisplayMemberPath="Value" TargetUpdated="ComboBox_TargetUpdated"
    StaysOpenOnEdit="True" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"/>
© www.soinside.com 2019 - 2024. All rights reserved.