簡體   English   中英

在WPF中可以綁定到DataContext中的屬性的屬性嗎?

[英]In WPF is it possible to bind to a property of a property in my DataContext?

我需要將TextBox的Text綁定到本身是DataContext屬性的類的屬性。 因此,例如,如果我有一個名為ServerInstance的類,並且它有一個名為Name的字符串屬性。 我在后面的代碼中公開了一個ServerInstance作為名為SelectedInstance的屬性(在這種情況下為viewmodel)。 我的問題是如何將TextBox綁定到SelectedInstance屬性的Name屬性?

這是我的ViewModel /代碼背后:

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>

預先感謝您提供的任何幫助。

{Binding Path=ServerInstance.Name}必須起作用,當然,即使ServerInstance應該是INotifyPropertyChanged也要看到名稱更改值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM