【问题标题】:Using list item or variable as source of Data Binding使用列表项或变量作为数据绑定的来源
【发布时间】:2012-08-19 21:35:05
【问题描述】:

我有一个关于在 WP7 中使用 c# 代码进行数据绑定的问题。

如果我有自定义类,一切都很清楚。我将 Source 属性设置为我的类实例,将 Path 属性设置为该类中的一个属性。像这样(并且有效)

Binding binding = new Binding()
{
  Source = myclass,
  Path = new PropertyPath("myproperty"),
  Mode = BindingMode.TwoWay
};
myButton.SetBinding(dp, binding);

现在,我如何绑定简单的结构,例如布尔变量或 List 中的单个项目? 如果我要写 Source = myBooleanSource = List[5] 我应该在 Path 属性中写什么? (请注意,我需要 TwoWay 绑定,因此必须设置 Path 属性)

最终解决方案:

要绑定一个变量,这个变量应该是一个公共属性,并且应该实现 INotifyPropertyChanged。为此,可以将 List 替换为 ObservableCollection。

其余的都应该是 nmaait 的答案,整个代码应该是这样的:

public partial class Main : PhoneApplicationPage, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Boolean _myBoolean { get; set; }
    public Boolean myBoolean
    {
        get { return _myBoolean; }
        set { _myBoolean = value; OnPropertyChanged("myBoolean"); }
    }
    ObservableCollection<Int32> myList { get; set; }

    public Main()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(Main_Loaded);
    }

    protected void OnPropertyChanged(String name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    void Main_Loaded(object sender, RoutedEventArgs e)
    {
        myBoolean = false;
        myList = new ObservableCollection<int>() { 100, 150, 200, 250 };


        Binding binding1 = new Binding()
        {
            Source = this,
            Path = new PropertyPath("myBoolean"),
            Mode = BindingMode.TwoWay
        };
        myButton.SetBinding(Button.IsEnabledProperty, binding1);


        Binding binding2 = new Binding()
        {
            Source = myList,
            Path = new PropertyPath("[1]"),
            Mode = BindingMode.TwoWay
        };

        myButton.SetBinding(Button.WidthProperty, binding2);
    }

    private void changeButton_Click(object sender, RoutedEventArgs e)
    {
        myList[1] +=50;
        myBoolean = !myBoolean;
    }
}

【问题讨论】:

    标签: c# windows-phone-7 data-binding


    【解决方案1】:

    您的源是数据上下文,因此您不会真正将源设置为布尔值本身,而是将源设置为布尔值所属的类/元素,就像您已经完成的那样。路径将是 myBoolean 或 List[5]。

    如果布尔值在当前类中,你可以这样做

    Binding binding = new Binding()
    {
      Source = this,
      Path = new PropertyPath("myBoolean"),
      Mode = BindingMode.TwoWay
    };
    myButton.SetBinding(dp, binding);
    

    您究竟想通过绑定到列表项来实现什么。如果您的列表发生更改,那么您不想绑定到特定索引,但您可以像这样绑定到所选项目。通过绑定到特定的列表项,提供有关您需要实现的更多信息。

    Binding binding = new Binding()
    {
      Source = List,
      Path = new PropertyPath("SelectedItem"),
      Mode = BindingMode.TwoWay
    };
    myButton.SetBinding(dp, binding);
    

    【讨论】:

    • 非常感谢,你帮了我很多)我已经在我的问题正文中添加了最终答案。 PS:我需要将数据绑定到 List 项目,因为我的页面上有一个非常困难的代码生成结构,并且要更新内容,只需将一些属性绑定到列表而不是每次都挖掘整个可视化树来查找/更改某些元素更容易.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多