2011-07-20 181 views
0

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

<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

你的XAML是充斥着错误和错误的用法,所有这些都是基本的知识不足的结果。请首先阅读关于数据绑定的介绍文档(可能从[本节]开始(http://msdn.microsoft.com/en-us/library/ms752347.aspx#creating_a_binding))。 – Jon

回答

0

1:<DataTemplate x:Key="GridItems" />是DataTemplate中,而不是一个ItemsSource即指定类型DataTemplate中的静态资源,然后用它。你应该删除这一行。

2:Source="{Binding必须具有ElementName,Source或RelativeSource来指定要绑定的元素。 Path是其元素的主观或领域。

3:从另一个类,而不是主窗口更新的ObservableCollection,你应该给此类引用它

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

,如果你在Q类中的任何行动elem将更新它的主窗口给它相应的对象。

+0

1.删除DataTemplate并更改ItemsSource =“{Binding GridItems}”,但不起作用。如果我在cs文件中的ItemsSource之前绑定GridItems = new ObservableCollection (); paysTable.ItemsSource = GridItems;这是有效的。如果我不绑定 - 不起作用 } – relief

+0

您应该设置相应的MainWindow的DataContext。基本上,它将是'Q'类的一个对象。然后你可以写'ItemsSource =“{Binding Elem}”',其中Elem - 获取'elem'对象的公共属性。 – stukselbax