如何绑定到嵌套类WPF [英] How to bind to a nested class wpf

查看:241
本文介绍了如何绑定到嵌套类WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为FieldViewModel的视图模型:

I have a view model called FieldViewModel:

public class FieldViewModel: INotifyPropertyChanged
{
    private Field m_field;
    public Field FieldData
    {
        get { return m_field; }
        set
        {
            m_field = value;
            NotifyPropertyChanged("FieldData");
        }
    }

和Field类:

public class Field : INotifyPropertyChanged
{     

    public List<string> SqlTypes
    {
        get { return Enum.GetNames(typeof(SqlDbType)).ToList(); }
    }


    private string m_FieldName = null;
    public string FieldName
    {
        get { return m_FieldName; }
        set
        {
            m_FieldName = value;
            NotifyPropertyChanged("FieldName");
        }
    }

    private string m_FieldType = null;
    public string FieldType
    {
        get { return m_FieldType; }
        set
        {
            m_FieldType = value;
            NotifyPropertyChanged("FieldType");
        }
    }

    private bool m_NullAllow = false;
    public bool NullAllow
    {
        get { return m_NullAllow; }
        set
        {
            m_NullAllow = value;
            NotifyPropertyChanged("NullAllowsChecked");
        }
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

Xaml:

<ComboBox ItemsSource="{Binding FieldData.SqlTypes}" SelectionChanged="cmbField_Selected" Width="20" Height="20" Margin="5,5,0,5" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <TextBox Width="120" Height="20" Text="{Binding FieldType}" Margin="0,5" VerticalAlignment="Top" HorizontalAlignment="Left"/>

现在在xaml中,我想绑定字段类的变量,但是我看不到它们

now in the xaml i want to bind the variables of the Field Class but i can't see them

我尝试了一些尝试,但没有任何效果,如果我将Field Class的变量放在FieldViewModel中,则效果很好.

I tried some thing but nothing works, if i put the variables of the Field Class inside the FieldViewModel it works good.

感谢您的帮助.

推荐答案

以下是对我有用的示例:

Here is an example which works for me:

<Window x:Class="DataGridExample.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">
<Grid>
    <DataGrid x:Name="czesciTable" ItemsSource="{Binding model.list}" 
              AutoGenerateColumns="False"
              CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Viewmodel:

Viewmodel:

public class ViewModel
{
    public Model model { get; set; }

    public ViewModel()
    {
        model = new Model();
    }
}

和型号:

public class Model 
{
    public List<String> list { get; set; }

    public Model()
    {
        list = new List<string>();

        list.Add("Item1");
        list.Add("Item2");
        list.Add("Item3");
    }
}

结果:

或组合框:

<ComboBox ItemsSource="{Binding model.list}" VerticalAlignment="Top"/> 

这篇关于如何绑定到嵌套类WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆