【问题标题】:Unable to fetch the property of another classe on the mainwindow.xaml textboxes无法在 mainwindow.xaml 文本框中获取另一个类的属性
【发布时间】:2013-02-02 14:58:00
【问题描述】:

我无法在 mainwindow.xaml 文本框上获取另一个类的属性。在 MainWindow.xaml 中,我试图获取在 mainwindow.xaml.cs 上定义的属性值。在这里我成功获取了第一个文本框中ABC类的名称属性。详情如下:

MainWindow.xaml

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid DataContext="{Binding}">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Vertical" Grid.Row="0">
        <TextBox  Text="{Binding Name1}"/>
        </StackPanel>
        <StackPanel  Orientation="Vertical" Grid.Row="1">
            <TextBox Text="{Binding class1.Name1}"/>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window,INotifyPropertyChanged
{
    Class1 c1 = new Class1();
    public MainWindow()
    {
        InitializeComponent();

ABC 赢 = 新 ABC(); win.Name1 = "主窗口";
c1.Name = "Class 1"; this.DataContext = 赢; }

    public class ABC
    {
        public string Name { get; set; }
    }

    public Class1 class1
    {
        get
        {
            return c1;
        }
        set
        {
            c1 = value;
            INotifyChanged("class1");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void INotifyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(name));
        }
    }
}  

Class1.cs

public class Class1:INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            NotifyPropertyChanged("Name");
        }
    }

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

【问题讨论】:

  • Class1 中没有名为Name1 的属性
  • 查看您的 Class1 它有 1 个名为 Name 的属性,因此您的 xaml 必须是 &lt;TextBox Text="{Binding class1.Name}"/&gt;
  • 是的,你是对的......这是错误的......现在我按照你说的做了......但它仍然没有显示我想要的
  • 您是否像我在回答中建议的那样修复了您的DataContext

标签: c# .net wpf xaml


【解决方案1】:

Class1 它有 1 个名为 Name 的属性,因此您的 xaml 必须是 &lt;TextBox Text="{Binding class1.Name}"/&gt;,并且您似乎将您的 DataContext 设置为嵌套类,您不能这样做,嵌套类不是xaml 中支持。

您必须将ABC 添加为变量并作为嵌套类删除

例子:

Xaml:

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid DataContext="{Binding ElementName=UI}">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>

        <!--You can't bind to a nested class-->
        <!--<StackPanel Orientation="Vertical" Grid.Row="0">
            <TextBox  Text="{Binding Name1}"/>
        </StackPanel>-->

        <StackPanel Orientation="Vertical" Grid.Row="0">
            <TextBox  Text="{Binding ABCClass.Name1}"/>
        </StackPanel>
        <StackPanel  Orientation="Vertical" Grid.Row="1">
            <TextBox Text="{Binding class1.Name}"/>
        </StackPanel>
    </Grid>
</Window

代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private Class1 c1 = new Class1();
    private ABC _abcClass = new ABC();

    public MainWindow()
    {
        InitializeComponent();
        class1.Name = "Class 1";
        _abcClass.Name1 = "ABC Class";
    }

    public ABC ABCClass
    {
        get { return _abcClass; }
        set { _abcClass = value; INotifyChanged("ABCClass"); }
    }

    public Class1 class1
    {
        get { return c1; }
        set { c1 = value; INotifyChanged("class1"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void INotifyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Class1 : NotifyBase
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; NotifyPropertyChanged("Name"); }
    }
}

public class ABC : NotifyBase
{
    private string name;
    public string Name1
    {
        get { return name; }
        set { name = value; NotifyPropertyChanged("Name1"); }
    }
}

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

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2018-05-22
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多