Binding a Collection to a DataGrid
I have next XAML. I have 3 issues: 1. my DataGrid doesn't see ObservableCollection. I set it by name in DataGrid XAML. and when I add new item to collection the DataGrid is not updated. Why? 2. Why my image Binding doesn't work? I don't see ImageConverter call in cs file 3. How to update ObservableCollection from another class rather than MainWindow?
<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>
</DataGridTempl开发者_运维百科ateColumn>
</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>
my server code
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: your <DataTemplate x:Key="GridItems" />
is DataTemplate, not an ItemsSource
i.e. you specified StaticResource of type DataTemplate, and then use it. you should delete this line.
2: Source="{Binding
has to have ElementName, Source or RelativeSource to specify what element you are going to bind. Path
is the propety or field of its element.
3: To update ObservableCollection from another class rather than MainWindow, you should to give this class reference to it
public class Q
{
ObsrvableCollection elem;
public Q (ObsrvableCollection _elem)
{
this.elem = _elem;
}
}
and any action in Q
class on elem
will update it in MainWindow, if you gave to it corresponding object.
精彩评论