c# - 如何绑定(bind)到嵌套类 wpf

标签 c# wpf xaml binding

我有一个名为 FieldViewModel 的 View 模型:

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 中我想绑定(bind) Field 类的变量但是我看不到它们

我尝试了一些方法,但没有任何效果,如果我将 Field 类的变量放在 FieldViewModel 中,效果很好。

感谢您的帮助。

最佳答案

这是一个对我有用的例子:

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

View 模型:

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 ItemsSource="{Binding model.list}" VerticalAlignment="Top"/> 

enter image description here

关于c# - 如何绑定(bind)到嵌套类 wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041542/

相关文章:

c# - 集成 Windows 身份验证破坏了 File.Exists

c# - WPF:如何在 MVVM 中播放 Storyboard?

c# - MVC 带有附加数据的多文件上传

c# - 使用EMGU的两个图像之间的相关性

c# - 如何在 UWP 中重新实例化页面/ View 模型?

c# - UWP XAML - 将 GridView 的大小调整为 ListViewItem 父级的完整宽度

c# - 如何创建弱引用事件处理程序?

c# - 如何将事件触发器添加到业务对象的数据模板?

c# - 数据网格中的 WPF 数据绑定(bind)组合框

c# - 如何在运行时修改xaml资源?