1

我正在尝试使用与 XAML 中对应的绑定创建 DataGrid。

 <WrapPanel Orientation="Vertical" Name="myWrapPanel" Margin="20,120,20,0">
    <DataGrid BorderBrush="{x:Null}" Name="myDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" AlternatingRowBackground="#8A2BE2" RowBackground="#FFA40000" Background="{x:Null}" CanUserAddRows="False" CanUserDeleteRows="True" HeadersVisibility="None" HorizontalAlignment="Left" Width="340" >
                <DataGrid.Columns>
                <DataGridTemplateColumn Width="10">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding Path=one, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="4*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock VerticalAlignment="Center" TextWrapping="NoWrap" HorizontalAlignment="Left" Name="TAG" Text="{Binding Path=two}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="5*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=three, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Right" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </WrapPanel>

不幸的是,动态创建的并不相同。复选框看起来不同,边距不正确。到目前为止,这是我的代码:

        DataGrid myDataGrid2 = new DataGrid();
        myDataGrid2.AutoGenerateColumns = false;
        myDataGrid2.AlternatingRowBackground = (Brush)(new BrushConverter().ConvertFrom("#8A2BE2"));
        myDataGrid2.RowBackground = (Brush)(new BrushConverter().ConvertFrom("#FFA40000"));
        myDataGrid2.CanUserAddRows = false;
        myDataGrid2.CanUserDeleteRows = true;
        myDataGrid2.HeadersVisibility = DataGridHeadersVisibility.None;
        myDataGrid2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        myDataGrid2.BorderThickness = new Thickness(0);

        Binding one = new Binding();
        one.Path = new PropertyPath("one");
        one.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        one.Mode = BindingMode.TwoWay;

        DataGridCheckBoxColumn One = new DataGridCheckBoxColumn();
        One.Width = 10;
        One.Binding = one;

        Binding two = new Binding();
        two.Path = new PropertyPath("two");
        two.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        two.Mode = BindingMode.OneWay;

        DataGridTextColumn Two = new DataGridTextColumn();
        Two.Width = new DataGridLength(4, DataGridLengthUnitType.Star);
        Two.Binding = two;

        Binding three = new Binding();
        three.Path = new PropertyPath("three");
        three.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        three.Mode = BindingMode.TwoWay;

        DataGridTextColumn Three = new DataGridTextColumn();
        Three.Width = new DataGridLength(5, DataGridLengthUnitType.Star);
        Three.Binding = three;

        myDataGrid2.Width = this.Width - 40;
        myDataGrid2.Height = myDataGrid2.RowHeight * myDataGrid2.Items.Count + 20;

        myDataGrid2.Columns.Add(One);
        myDataGrid2.Columns.Add(Two);
        myDataGrid2.Columns.Add(Three);
        myWrapPanel.Children.Add(myDataGrid2);

        myDataGrid2.ItemsSource = new ObservableCollection<Driver>(driver);

这是我在 WPF 中的第一个应用程序。有什么帮助吗?

Bindig 效果很好,只有视觉效果才是问题。 http://imageshack.us/photo/my-images/194/8iok.png/ 如图所示,上半部分是 XAML,下半部分是 C#。我想在一些线程结束后动态添加一些数据。这是重点。

4

2 回答 2

2

我可能会Style使用设置的属性创建一个,然后将其用于myDataGridmyDataGrid2。例如:

// in current file's Resources section
<Style x:Key="MyDataGridStyle">
    <Setter Property="BorderBrush" Value="{x:Null}" />
    // other stuff...
    <Setter Property="Columns">
        <Setter.Value>
                <DataGridTemplateColumn Width="10">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding Path=one, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="4*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock VerticalAlignment="Center" TextWrapping="NoWrap" HorizontalAlignment="Left" Name="TAG" Text="{Binding Path=two}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="5*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=three, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Right" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
        </Setter.Value>
    </Setter>
 </Style>


 <WrapPanel Orientation="Vertical" Name="myWrapPanel" Margin="20,120,20,0">
    <DataGrid Name="myDataGrid" Style="{StaticResource MyDataGridStyle}" />
 </WrapPanel>

DataGrid myDataGrid2 = new DataGrid();
myDataGrid2.Style = (Style)this.Resources["MyDataGridStyle"];
于 2013-08-26T15:56:27.660 回答
0

什么不工作?绑定?视觉上的数据网格?

请更具体。

从我在你的代码中看到的一切看起来都是正确的。您可以尝试手动添加项目(手动将它们添加到 myDataGrid2.Items)吗?

除此之外,两种方式的索引也存在一些问题,这可能是您的情况(请参阅WPF - 以编程方式绑定到 DataGridCheckBoxColumn

于 2013-08-26T16:06:49.377 回答