gpt4 book ai didi

c# - 数据绑定(bind)到嵌套属性?

转载 作者:太空狗 更新时间:2023-10-29 21:49:58 26 4
gpt4 key购买 nike

我是 WPF 和 XAML 的新手,现在我被数据绑定(bind)困扰了好几天!我只是想将一些嵌套属性绑定(bind)到 TextBox 和 ListView(通过 XAML),但我做错了。这是我的示例代码:

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>

我的问题:

  1. 如何绑定(bind)嵌套属性?是否可以?为什么像

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

    不工作?

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

最佳答案

像这样在构造函数中设置数据上下文:

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

当然,您的数据路径也会发生变化,因为您绑定(bind)的数据位于 MyModel 下。您的绑定(bind)应更改如下:

<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>

编辑:

这就是您使用 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>

代码修改如下:

 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();
}
}
}

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

关于c# - 数据绑定(bind)到嵌套属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com