数据绑定到嵌套属性? [英] Data Binding to Nested Properties?

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

问题描述

我对WPF和XAML非常新鲜,现在我几天坚持数据绑定!我只想将一些嵌套的属性绑定到一个TextBox和ListView(通过XAML),但我做错了。
这是我的示例代码:



MainWindow.xaml.cs

 命名空间CounterTestNestedDataBinding 
{
public partial class MainWindow:Window
{
public MyModel MyModel {get;组; }

public MainWindow()
{
InitializeComponent();
MyModel = new MyModel {MyCounter = new Counter()};
}

private void Button_Click(object sender,RoutedEventArgs e)
{
MyModel.MyCounter.incrementCounter();
}
}
}

MyModel.cs

 命名空间CounterTestNestedDataBinding 
{
public class MyModel:INotifyPropertyChanged
{
public Counter _myCounter;
public Counter MyCounter
{
get {return _myCounter; }
set
{
_myCounter = value;
NotifyPropertyChanged(MyCounter);
}
}

//其他一些成员和属性...

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs(property));
}
}

}
}

Counter.cs

 命名空间CounterTestNestedDataBinding 
{
public class计数器:INotifyPropertyChanged
{
#region成员
private int _currentNumber;
private ObservableCollection< int> _historyList;
#endregion

#region构造函数
public Counter()
{
_currentNumber = 0;
_historyList = new ObservableCollection< int>();
}
#endregion

#region属性
public int CurrentNumber
{
get {return _currentNumber; }
set
{
_currentNumber = value;
NotifyPropertyChanged(CurrentNumber);
}
}

public ObservableCollection< int> HistoryList
{
get {return _historyList;
set
{
_historyList = value;
NotifyPropertyChanged(HistoryList);
}
}
#endregion

public void incrementCounter()
{
HistoryList.Add(CurrentNumber);
CurrentNumber ++;
}

public override string ToString()
{
return string.Format(CurrentNumber:{0},HistoryList:{1},_currentNumber,String .Join(,,_historyList));
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs(property));
}
}
}
}

strong> MainWindow.xaml

 < Window x:Class =CounterTestNestedDataBinding.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns :counterTestNestedDataBinding =clr-namespace:CounterTestNestedDataBinding
Title =MainWindowHeight =350Width =200ResizeMode =NoResizeWindowStartupLocation =CenterScreen
DataContext ={Binding RelativeSource = {RelativeSource Self}}
>
< StackPanel Orientation =Vertical>
< TextBox x:Name =TextBoxCounterCurrentText ={Binding MyModel.MyCounter.CurrentNumber}/>
< Button Content =ButtonClick =Button_Click/>
< ListView x:Name =ListViewCounterHistoryHeight =75ItemsSource ={Binding MyModel.MyCounter.HistoryList}>< / ListView>
< / StackPanel>
< / Window>

我的问题:


  1. 如何绑定嵌套属性?可能吗?为什么要这样的东西,例如

      Text ={Binding MyModel.MyCounter.CurrentNumber}

    不工作?


  2. XAML中的DataContext设置是否正确?



解决方案

在构造函数中设置您的数据上下文:

  public MainWindow()
{
InitializeComponent();
MyModel = new MyModel {MyCounter = new Counter()};
this.DataContext = MyModel;
}

然后当然您的数据路径会因为您所在的数据而改变绑定在MyModel下。您的绑定应更改如下:

 < StackPanel Orientation =Vertical> 
< TextBox x:Name =TextBoxCounterCurrentText ={Binding MyCounter.CurrentNumber}/>
< Button Content =ButtonClick =Button_Click/>
< ListView x:Name =ListViewCounterHistoryHeight =75ItemsSource ={Binding MyCounter.HistoryList}>< / ListView>
< / StackPanel>

编辑:



通过使用XAML来实现。

 < Window x:Class =WpfApplication1.MainWindow
xmlns =http ://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local = clr-namespace:WpfApplication1
xmlns:system =clr-namespace:System; assembly = mscorlib
Title =MainWindowHeight =350Width =525>
< Window.Resources>
< local:MyModel x:Key =myModal/>
< /Window.Resources>

< StackPanel Orientation =VerticalDataContext ={StaticResource myModal}>
< TextBox x:Name =TextBoxCounterCurrentText ={Binding MyCounter.CurrentNumber}/>
< Button Content =ButtonClick =Button_Click/>
< ListView x:Name =ListViewCounterHistoryHeight =75ItemsSource ={Binding MyCounter.HistoryList}>< / ListView>
< / StackPanel>



和代码更改如下:

  public partial class MainWindow:Window 
{
public MyModel MyModel {get;组; }
public MainWindow()
{
InitializeComponent();
// MyModel = new MyModel {MyCounter = new Counter()};
//this.DataContext = MyModel;
}

private void Button_Click(object sender,RoutedEventArgs e)
{
var myModel = this.Resources [myModal] as MyModel;
if(myModel!= null)
{
myModel.MyCounter.incrementCounter();
}
}
}

顺便说一句,你应该初始化MyModel构造函数中的 _myCounter


I'm pretty new to WPF and XAML and now I'm stuck with data binding for days! I just wanted to bind some nested properties to a TextBox and ListView (via XAML), but I'm doing it wrong. Here's my Sample Code:

MainWindow.xaml.cs

namespace CounterTestNestedDataBinding
{
    public partial class MainWindow : Window
    {
        public MyModel MyModel { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            MyModel = new MyModel { MyCounter = new Counter() };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MyModel.MyCounter.incrementCounter();
        }
    }
}

MyModel.cs

namespace CounterTestNestedDataBinding
{
    public class MyModel : INotifyPropertyChanged
    {
        public Counter _myCounter;
        public Counter MyCounter
        {
            get { return _myCounter; }
            set
            {
                _myCounter = value;
                NotifyPropertyChanged("MyCounter");
            }
        }

        // some other members and properties ...

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

    }
}

Counter.cs

namespace CounterTestNestedDataBinding
{
    public class Counter : INotifyPropertyChanged
    {
        #region Members
        private int _currentNumber;
        private ObservableCollection<int> _historyList;
        #endregion

        #region Constructor
        public Counter()
        {
            _currentNumber = 0;
            _historyList = new ObservableCollection<int>();
        }
        #endregion

        #region Properties
        public int CurrentNumber
        {
            get { return _currentNumber; }
            set
            {
                _currentNumber = value;
                NotifyPropertyChanged("CurrentNumber");
            }
        }

        public ObservableCollection<int> HistoryList
        {
            get { return _historyList; }
            set
            {
                _historyList = value;
                NotifyPropertyChanged("HistoryList");
            }
        }
        #endregion

        public void incrementCounter()
        {
            HistoryList.Add(CurrentNumber);
            CurrentNumber++;
        }

        public override string ToString()
        {
            return string.Format("CurrentNumber: {0}, HistoryList: {1}", _currentNumber, String.Join(",", _historyList));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}

MainWindow.xaml

<Window x:Class="CounterTestNestedDataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:counterTestNestedDataBinding="clr-namespace:CounterTestNestedDataBinding"
        Title="MainWindow" Height="350" Width="200" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
    <StackPanel Orientation="Vertical">
        <TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyModel.MyCounter.CurrentNumber}"/>
        <Button Content="Button" Click="Button_Click"/>
        <ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyModel.MyCounter.HistoryList}"></ListView>
    </StackPanel>
</Window>

My questions:

  1. How can I bind nested properties? Is it possible? Why does something like

    Text="{Binding MyModel.MyCounter.CurrentNumber}"
    

    not work?

  2. Is the "DataContext" set correct in the XAML?

解决方案

Set your data context in the constructor like this:

public MainWindow()
{
    InitializeComponent();
    MyModel = new MyModel { MyCounter = new Counter() };
    this.DataContext = MyModel;
}

and then of course the path to your data changes because the data that you are binding is under MyModel. Your bindings should be changed as follows:

<StackPanel Orientation="Vertical">
    <TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyCounter.CurrentNumber}"/>
    <Button Content="Button" Click="Button_Click"/>
    <ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyCounter.HistoryList}"></ListView>
</StackPanel>

EDIT:

This is how you do it by using XAML.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
    <local:MyModel x:Key="myModal" />
</Window.Resources>

<StackPanel Orientation="Vertical" DataContext="{StaticResource myModal}">
    <TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyCounter.CurrentNumber}"/>
    <Button Content="Button" Click="Button_Click"/>
    <ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyCounter.HistoryList}"></ListView>
</StackPanel>

and code changes like this:

 public partial class MainWindow : Window
{
    public MyModel MyModel { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        //MyModel = new MyModel { MyCounter = new Counter() };
        //this.DataContext = MyModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var myModel = this.Resources["myModal"] as MyModel;
        if (myModel != null)
        {
            myModel.MyCounter.incrementCounter();
        }
    }
}

By the way you should initialize _myCounter in MyModel constructor.

这篇关于数据绑定到嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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