将集合绑定到DataGrid

时间:2011-07-20 09:57:26

标签: wpf

我有下一个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"
            });
        }
}

1 个答案:

答案 0 :(得分:0)

1:您的<DataTemplate x:Key="GridItems" />是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;
     }
}

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