0

xaml datagrid 下面给了我两个额外的列,在 Image 列之前和 price 列之后。我怎样才能禁用它?

<DataGrid Name="dgProductList" AutoGenerateColumns="False" FontSize="18" 
                                  ScrollViewer.CanContentScroll="False"      
                                  ScrollViewer.VerticalScrollBarVisibility="Auto" 
                                  ScrollViewer.HorizontalScrollBarVisibility="Auto">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Image">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Image Height="10" Width="10" Source="{Binding Picture}" Stretch="Uniform"></Image>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Binding="{Binding Name}" Header="Item" />
                                <DataGridTextColumn Header="Qty" />
                                <DataGridTextColumn Binding="{Binding Price}" Header="Price" />
                            </DataGrid.Columns>
                        </DataGrid>

谢谢你。

4

2 回答 2

3

在 DataGrid 中添加 itemssource 绑定:

<DataGrid Name="dgProductList" AutoGenerateColumns="False" FontSize="18"
                              ScrollViewer.CanContentScroll="False"
                              ScrollViewer.VerticalScrollBarVisibility="Auto"
                              ScrollViewer.HorizontalScrollBarVisibility="Auto"
          ItemsSource="{Binding Products}" MouseDoubleClick="dgProductList_MouseDoubleClick">

代码隐藏:

public class Product : NotificationObject
{
    public string Name { get; set; }
}

public IList<Product> Products { get; set; }

public MainWindow()
{
    Products = new ObservableCollection<Product>
    {
        new Product {Name = "test1"},
        new Product {Name = "test2"},
    };

    DataContext = this;

    InitializeComponent();
}

private void dgProductList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    Products.Add(new Product { Name = "test x" });
}
于 2012-09-26T09:05:46.200 回答
1

如果我只是复制并粘贴您的 xaml,则没有额外的列。也许在创建列的代码隐藏文件中有一些代码?

于 2012-09-26T08:42:26.630 回答