如何从XAML访问代码中的对象

时间:2013-03-04 21:51:15

标签: c# wpf xaml data-binding

我是WPF的新手,我正在尝试理解如何使用数据绑定将我窗口上的控件绑定到我后面的代码中的对象。我看到几个关于从代码隐藏中访问XAML对象的问题,但这不是我正在寻找的。我已经知道该怎么做了。

label1.Content = LabelText;
listbox1.ItemsSource = ListItems;

我也看到过如何从XAML访问代码隐藏中的类的答案。

<local:MyClass x:Key="myClass" />

但我不知道如何将其应用于该类的特定实例。这是我正在尝试做的一个例子。 '绑定'显然是不正确的。这就是我需要帮助的地方。

public partial class MainWindow : Window
{
    private string _labelText;
    private List<string> _listItems = new List<string>();

    public MainWindow()
    {
        InitializeComponent();

        _labelText = "Binding";
        _listItems.Add("To");
        _listItems.Add("An");
        _listItems.Add("Object");
    }

    public string LabelText
    {
        get { return _labelText; }
        set { _labelText = value; }
    }

    public List<string> ListItems
    {
        get { return _listItems; }
        set { _listItems = value; }
    }
}

<Window x:Class="SO_Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SO Demo" Height="160" Width="225">
  <Grid DataContext="MainWindow">
    <Label x:Name="label1" Width="80" Height="25" Margin="12,12,0,0" 
           Content="{Binding Path=LabelText}"
           HorizontalAlignment="Left" VerticalAlignment="Top" />
    <ListBox x:Name="listbox1" Width="100" Height="60" Margin="12,44,0,0" 
             ItemsSource="{Binding Path=ListItems}" DisplayMemberPath="ListItems"
             HorizontalAlignment="Left" VerticalAlignment="Top" />
  </Grid>
</Window>

我读过的书籍和教程听起来应该很简单。我错过了什么?

2 个答案:

答案 0 :(得分:4)

虽然你可以按照你正在尝试的方式DataBind直接进入课堂,但通常不会这样做。建议的方法是创建一个对象(ViewModel),该对象聚合您希望在UI中显示的所有模型数据,然后将该ViewModel设置为View的DataContext(在本例中为Window)。我建议阅读MVVM,这是大多数WPF应用程序的构建方式。但是下面的例子可以帮助你开始。

以下是基于上述示例的简单示例:

<强>视图模型

public class MyViewModel : INotifyPropertyChanged
{
    private string _title;
    private ObservableCollection<string> _items;

    public string LabelText
    { 
        get { return _title; } 
        set 
        { 
            _title = value;
            this.RaisePropertyChanged("Title");
        }
    }

    public ObservableCollection<string> ListItems { 
        get { return _items; }
        set 
        { 
            _items = value;   //Not the best way to populate your "items", but this is just for demonstration purposes.
            this.RaisePropertyChanged("ListItems");
        }
    }

    //Implementation of INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

<强>代码隐藏

public partial class MainWindow : Window
{
    private MyViewModel _viewModel;

    public MainWindow()
    {
        InitializeComponent();
        _viewModel = new MyViewModel();

        //Initialize view model with data...

        this.DataContext = _viewModel;
    }
}

查看(窗口)

<Window x:Class="SO_Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SO Demo" Height="160" Width="225">
  <Grid>
    <Label x:Name="label1" Width="80" Height="25" Margin="12,12,0,0"               Content="{Binding Path=LabelText}"
           HorizontalAlignment="Left" VerticalAlignment="Top" />
    <ListBox x:Name="listbox1" Width="100" Height="60" Margin="12,44,0,0" 
             ItemsSource="{Binding Path=ListItems}"
             HorizontalAlignment="Left" VerticalAlignment="Top" />
  </Grid>
</Window>

答案 1 :(得分:3)

<Grid DataContext="MainWindow">无效。

如果您想引用该窗口,您必须:

<Window x:Name="MyWindow">
   <Grid DataContext="{Binding ElementName=MyWindow}"/>
</Window>

<Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>