在WPF中是否可以绑定到我的DataContext中的属性的属性?

时间:2011-03-24 16:35:11

标签: c# wpf data-binding

我需要将TextBox的Text绑定到类的属性,该类本身就是我的DataContext的属性。所以,例如,如果我有一个名为ServerInstance的类,它有一个名为Name的字符串属性。我将ServerInstance作为一个名为SelectedInstance的属性暴露在我的代码后面(在本例中为viewmodel)。我的问题是如何将TextBox绑定到SelectedInstance属性的Name属性?

这是我的ViewModel / Code背后:

public class ViewModel : Notifier
{
    private MobileServerInstance m_instance = null;
    private RelayCommand m_addCommand = null;
    private RelayCommand m_clearCommand = null;

    public MobileServerInstance ServerInstance
    {
        get { return m_instance; }
        set { m_instance = value; OnPropertyChanged("ServerInstance"); }
    }

    public ICommand AddCommand
    {
        get
        {
            if (m_addCommand == null)
            {
                m_addCommand = new RelayCommand(parameter=>Add(parameter), parameter=>CanAdd(parameter));
            }

            return m_addCommand;
        }
    }

    public ICommand ClearCommand
    {
        get
        {
            if (m_clearCommand == null)
            {
                m_clearCommand = new RelayCommand(parameter => Clear(parameter), parameter => CanClear(parameter));
            }

            return m_clearCommand;
        }
    }

    private bool CanClear(object parameter)
    {
        return m_instance != null;
    }

    private void Clear(object parameter)
    {
        m_instance = null;
    }

    private bool CanAdd(object parameter)
    {
        return m_instance == null;
    }

    private void Add(object parameter)
    {
        m_instance = new MobileServerInstance();
    }
}

Notifier是一个实现INotifyPropertyChanged接口的基类,它提供了一个受保护的OnPropertyChanged方法,该方法引发了PropertyChange事件 - 那里的典型模式。

这是我用于DataContext的简单类:

public class MobileServerInstance : Notifier
{
    private string m_name = "Name";
    private string m_alias = "Alias";

    public string Name
    {
        get { return m_name; }
        set { m_name = value; OnPropertyChanged("Name"); }
    }

    public string Alias
    {
        get { return m_alias; }
        set { m_alias = value; OnPropertyChanged("Alias"); }
    }
}

最后我的xaml(这是一个POC所以UI非常简单):

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="500">

    <Window.DataContext>
        <src:ViewModel />
    </Window.DataContext>

    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Name:" Margin="0 0 2 0" />
            <ComboBox Margin="0 0 8 0" Width="125" />
            <Button Margin="0 0 4 0" Content="Add" Command="{Binding AddCommand}" Width="75" />
            <Button Content="Clear" Command="{Binding ClearCommand}" Width="75" />
        </StackPanel>

        <StackPanel Margin="20 5 20 5"">
            <StackPanel Orientation="Horizontal">
                <Label Width="40" Content="Name:" Margin="0 0 2 0" />
                <TextBox Width="250" Text="{Binding ServerInstance.Name}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0 5 0 0">
                <Label Width="40" Content="Alias:" Margin="0 0 2 0" />
                <TextBox Width="250" Text="{Binding ServerInstance.Alias}" />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Window>

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:5)

{Binding Path=ServerInstance.Name}必须正常工作,当然要查看名称更改值,即使ServerInstance应该是INotifyPropertyChanged