C#、WPF、将 List<string> 绑定到 DataGrid

问题描述 投票:0回答:3

我无法将列表绑定到 DataGrid。它应该尽可能简单。我是 WPF 新手,这是为了我的个人教育。

我有一个View(Editor),ViewModel(VMText)和一个Data(JustText)类。

到目前为止我的来源:

JustText.cs

namespace Model
{
    public class Text
    {
        private string _code;
        public string Code
        {
            get { return _code;  }
            set { _code = value; }
        }

        public Text()
        {
            _code = "Hello World!\nHow you doin'?";
        }
    } 
}

VMText.cs

namespace ViewModel
{
    public class VMText
    {    
        private Model.Text _code;

        public List<string> Code
        {            
            get { return new List<string>(_code.Code.Split('\n'));        }
            set { _code.Code = System.String.Join("\n", value.ToArray()); }
        }

        private View.Editor editor;

        public VMText(View.Editor editor)
        {
            _code = new Model.Text();
            this.editor = editor; 
        }
    }
}

编辑器.xaml

<Window x:Class="View.Editor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:View"
        mc:Ignorable="d"
        Title="Editor" Height="240.024" Width="269.895">
    <Grid Background="#FF292929" Margin="0,0,-6.8,0.4">
        <DataGrid x:Name="dataGrid" 
                  HorizontalAlignment="Left" 
                  Margin="0,0,0,0" 
                  VerticalAlignment="Top"
                  Width="200pt"
                  Height="100pt"
                  DataContext="{Binding vmText}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Code, UpdateSourceTrigger=PropertyChanged}" Foreground="Black" Width="60" Header="Test" IsReadOnly="false" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

编辑器.xaml.cs

namespace View
{
    public partial class Editor : Window
    {
        private ViewModel.VMText vmText;

        #region Constructor

        public Editor()
        {
            InitializeComponent();

            vmText = new ViewModel.VMText(this);
            DataContext = vmText;
        }

        #endregion
    }
}

我只想在 DataGrid 的一列中显示在 VMText 中创建的列表

c# wpf xaml binding datagrid
3个回答
16
投票

我猜您只是想在 DataGrid 中显示视图模型的代码源集合属性中的字符串。你应该

  • 然后将 DataGrid 的
    ItemsSource
    属性绑定到视图模型的 Code source 属性
  • 然后将 DataGridTextColumn 绑定到代码列表本身中的字符串。

您只需稍微修改视图的 XAML 标记就可以看到字符串。试试这个:

<DataGrid x:Name="dataGrid" 
        HorizontalAlignment="Left" 
        Margin="0,0,0,0" 
        VerticalAlignment="Top"
        Width="200pt"
        Height="100pt"
        ItemsSource="{Binding Code}"
        AutoGenerateColumns="False">
   <DataGrid.Columns>
   <DataGridTextColumn 
        Header="Test" IsReadOnly="false" 
        Binding="{Binding}" 
        Foreground="Black" 
        Width="60" />
   </DataGrid.Columns>
</DataGrid>

3
投票

您应该实现 INotifyPropertyChanged 来通知绑定属性已更改。另外,对于集合,请查看

ObservableCollection
而不是
List


1
投票

将 VMText.Code 绑定到 DataGrid ItemSource。当您在代码后面执行此操作时,不需要在视图中初始化 DataGrid DataContract。

视图模型

namespace ViewModel
{
    public class VMText : INotifyPropertyChanged
    {
        public VMText(View.Editor editor)
        {
            _code = new Model.Text();
            this.editor = editor; 
        }

        public List<string> Code
        {            
            get
            {
                return new List<string>(_code.Code.Split('\n'));
            }
            set
            {
                _code.Code = System.String.Join("\n", value.ToArray());
                NotifyPropertyChanged("Code");
            }
        }

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

        public event PropertyChangedEventHandler PropertyChanged;

        private Model.Text _code;

        private View.Editor editor;
    }
}

查看

<Window x:Class="View.Editor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:View"
    mc:Ignorable="d"
    Title="Editor" Height="240.024" Width="269.895">
<Grid Background="#FF292929" Margin="0,0,-6.8,0.4">
    <DataGrid x:Name="dataGrid" 
              HorizontalAlignment="Left" 
              Margin="0,0,0,0" 
              VerticalAlignment="Top"
              Width="200pt"
              Height="100pt"
              ItemsSource="{Binding Code}>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Code"
                                Foreground="Black" Width="60"
                                Header="Test"
                                IsReadOnly="false" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>
© www.soinside.com 2019 - 2024. All rights reserved.