将 DataGrid 绑定到另一个对象中的字符串的可观察集合
本文关键字:字符串 观察 集合 一个对象 DataGrid 绑定 | 更新日期: 2023-09-27 17:56:34
我有一个包含可观察字符串集合的对象,如何绑定数据网格以显示这些字符串?
举个例子:
public class Container
{
public ObservableCollection<string> strs; //This won't work, see edit!
.
.
.
}
XAML:
<DataGrid ItemsSource="{Binding Container}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Strings" Binding="{Binding}" />
</DataGrid.Columns>
</Datagrid>
为搜索者编辑:上述方法存在一些问题,首先您可以通过简单地引用这些属性来绑定到项目的属性。在这种情况下:
ItemsSource="{Binding Container.strs}"
其次,字符串的内容不是字符串的属性,因此
Binding="{Binding}"
直接绑定到字符串,而不是尝试查找其属性(如 Length)
最后你不能绑定到字段,只能绑定到属性,有什么区别?
public ObservableCollection<string> strs; //This is a field
public ObservableCollection<string> strs {get; set;} //This is property
奖励:如果您只是实例化一次 strs,那么 ObservableCollection 将在更改为/在其内发生时通知其绑定的任何内容,但如果您要更改指针,它不会,要解决此问题,您可以使用依赖属性!
在Visual Studio中,最好为此使用内置的代码片段,因为有很多东西需要填写类型:"propdp"并按两次选项卡,在这种情况下,我们将有:
public ObservableCollection<string> strs
{
get { return (ObservableCollection<string>)GetValue(strsProperty); }
set { SetValue(strsProperty, value); }
}
// Using a DependencyProperty as the backing store for strs. This enables animation, styling, binding, etc...
public static readonly DependencyProperty strsProperty =
DependencyProperty.Register("strs", typeof(ObservableCollection<string>), typeof(Container), new PropertyMetadata(""));
这对
我有用。
XAML:
<Window x:Class="WpfApplication2.MainWindow"
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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding thing.stuff}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="Auto" Header="String Contents" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
和代码隐藏 (C#):
using System.Windows;
using System.Collections.ObjectModel;
namespace WpfApplication2
{
public class Thing
{
// make sure this is a property, not a field.
// furthermore, make sure it is public.
public ObservableCollection<string> stuff
{
get; set;
}
public Thing()
{
stuff = new ObservableCollection<string>();
stuff.Add("A String");
stuff.Add("Another String");
stuff.Add("Yet Another String");
}
}
public partial class MainWindow : Window
{
public Thing thing{get;set;}
public MainWindow()
{
InitializeComponent();
DataContext = this;
thing = new Thing();
}
}
}
我建议您进一步充实您的问题。请记住,StackOverflow的目标是问题对其他用户以及您自己都有帮助。
编辑:Terser XAML
<Window x:Class="WpfApplication2.MainWindow"
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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding thing.stuff}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="Auto" Binding="{Binding}" Header="String Contents" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>