如何绑定到嵌套类wpf

时间:2015-06-25 04:59:32

标签: c# wpf xaml binding

我有一个名为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中我想绑定Field Class的变量,但我看不到它们

我尝试过一些东西,但没有任何作用,如果我把Field Class的变量放在FieldViewModel里面就可以了。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这是一个适合我的例子:

<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>

视图模型:

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");
    }
}

结果:

enter image description here

或ComboBox:

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

enter image description here

答案 1 :(得分:0)

问题中的代码主要是正确的。它只是缺少Field Class的初始化。