【问题标题】:Binding a Collection to a DataGrid将集合绑定到 DataGrid
【发布时间】:2011-07-20 09:57:26
【问题描述】:

我有下一个 XAML。 我有3个问题: 1. 我的 DataGrid 没有看到 ObservableCollection。我在 DataGrid XAML 中按名称设置它。当我将新项目添加到集合时,DataGrid 不会更新。为什么? 2. 为什么我的图片绑定不起作用?我在 cs 文件中没有看到 ImageConverter 调用 3. 如何从另一个类而不是 MainWindow 更新 ObservableCollection?

<Window x:Class="ReikartzDataConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ReikartzDataConverter.img" Title="MainWindow" Height="650" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="GridItems" />
        <local:ImageConverter x:Key="ImageConverter"></local:ImageConverter>
    </Window.Resources>
    <Grid Width="780" Height="650">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Content="Process information" Height="28" HorizontalAlignment="Left" Margin="0,20,0,0" Name="label1" VerticalAlignment="Top" Width="235" />
        <DataGrid Grid.Row="1" Width="780" Name="paysTable" Background="AntiqueWhite" AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource GridItems}}">  
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="EnitityType" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=EntityType}">
                            </Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="EnitityId" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=EnitityId}">
                            </Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="EventName" Width="200">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=EventName}">
                            </Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="EventMessage" Width="300">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=EventMessage}">
                            </Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=/img/btn_delete.gif, Converter={StaticResource ImageConverter}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Label Grid.Row="2" Height="28" Name="lblError" VerticalAlignment="Top" Visibility="Hidden" Foreground="OrangeRed" FontWeight="Bold" FontSize="12" />
        <Button Grid.Row="3" Content="Quit" Height="23" Name="button1" Margin="200 0 0 0" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Grid.Row="3" Content="Start" Height="23" Name="button2" Margin="150 0 200 0" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    </Grid>
</Window>

我的服务器代码

 public partial class MainWindow : Window
    {
        /// <summary>
        /// the grid data source
        /// </summary>
        public ObservableCollection<DataGridItem> GridItems
        {
            get;
            set;
        }

  public MainWindow()
        {
            InitializeComponent();

            InitDataSource();
        }

        private void InitDataSource()
        {
            GridItems = new ObservableCollection<DataGridItem>();        
        }


       private void button2_Click(object sender, RoutedEventArgs e)
        {
            GridItems.Add(new DataGridItem
            {
                EnitityId = 1,
                EnitityType = "new",
                EventMessage = "microsoft",
                EventName = "must"
            });
        }
}

【问题讨论】:

  • 您的 XAML 充满了错误和误用,所有这些都是基础知识不足的结果。请先阅读有关数据绑定的介绍文档(可能从this section 开始)。

标签: wpf


【解决方案1】:

1:您的&lt;DataTemplate x:Key="GridItems" /&gt; 是DataTemplate,而不是ItemsSource,即您指定了DataTemplate 类型的StaticResource,然后使用它。你应该删除这一行。

2: Source="{Binding 必须有 ElementName、Source 或 RelativeSource 来指定您要绑定的元素。 Path 是其元素的属性或字段。

3:要从另一个类而不是 MainWindow 更新 ObservableCollection,你应该给这个类引用它

public class Q
{
     ObsrvableCollection elem;
     public Q (ObsrvableCollection _elem)
     {
         this.elem = _elem;
     }
}

如果你给它相应的对象,elem 上的 Q 类中的任何操作都会在 MainWindow 中更新它。

【讨论】:

  • 1.删除了 DataTemplate 并更改了 ItemsSource="{Binding GridItems}" 但不起作用。如果我在 cs 文件中的 ItemsSource 之前绑定 GridItems = new ObservableCollection(); paysTable.ItemsSource = GridItems;那是有效的。如果我不绑定 - 不起作用}
  • 你应该设置MainWindow对应的DataContext。基本上,它将是Q 类的对象。然后你可以写ItemsSource="{Binding Elem}",其中Elem - public property,得到elem对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2014-10-05
  • 2016-10-08
  • 2011-01-02
相关资源
最近更新 更多