我正在尝试使用与 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#。我想在一些线程结束后动态添加一些数据。这是重点。