我有一个Wpf DatarGrid,它绑定到客户列表。在这里,我想要的是当我在datagrid中选择一个客户并单击“更新”按钮时,将打开一个窗口。这个新窗口有一个标签控件。然后,当我第二次单击datagrid中另一个用户的更新时,第二个选项卡应在此已打开的新窗口中打开。为此,我想将Tab控件仅绑定到Selected Customer行,而不是ObservableCollection或List。但是,当我将SelectedCustomer绑定到Itemssource属性时,由于标题不起作用,因此它不起作用。对于标题,我有一个Itemtemplate,在其中将文本块绑定到SelectedCustomer属性(例如name)。
对此有什么解决方案?
我的数据网格看起来像这样-
<DataGrid Width="700" Height="500" Margin="20" AutoGenerateColumns="False" IsReadOnly="True" SelectedItem="{Binding SelectedCustomer}" ItemsSource="{Binding Customers}">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></DataGridTextColumn>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></DataGridTextColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="5" Click="Update_Clicked">Update</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="5">Delete</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
在这里您可以看到selecteditem属性绑定到SelectedCustomer。现在选项卡控制窗口看起来像这样-
<TabControl Name="UserTabs" ItemsSource="{Binding}">
<TabControl.ItemTemplate>
<DataTemplate DataType="TabItem">
<TextBlock Text="{Binding FirstName}"></TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<Grid Height="226" Margin="4">
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="FontSize" Value="20"></Setter>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0">First Name :</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="1">Last Name :</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="2">Email Address :</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Left"><Hyperlink>Address :</Hyperlink></TextBlock>
<TextBlock Grid.Column="0" Grid.Row="4">City :</TextBlock>
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}"></TextBox>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding LastName}"></TextBox>
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding EmailAddress}"></TextBox>
<TextBox Grid.Column="1" Grid.Row="4" Text="{Binding City}"></TextBox>
<TextBlock Grid.Column="2" Grid.Row="0">State :</TextBlock>
<TextBlock Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center"><Hyperlink>Orders :</Hyperlink></TextBlock>
<TextBlock Grid.Column="2" Grid.Row="2"></TextBlock>
<TextBlock Grid.Column="2" Grid.Row="3"></TextBlock>
<TextBlock Grid.Column="2" Grid.Row="4"></TextBlock>
<TextBlock Grid.Column="2" Grid.Row="5"></TextBlock>
<TextBox Grid.Column="3" Grid.Row="0" Text="{Binding State}"></TextBox>
<TextBlock Grid.Column="3" Grid.Row="1"></TextBlock>
<TextBlock Grid.Column="3" Grid.Row="2"></TextBlock>
<TextBlock Grid.Column="3" Grid.Row="3"></TextBlock>
<TextBlock Grid.Column="3" Grid.Row="4"></TextBlock>
<TextBlock Grid.Column="3" Grid.Row="5"></TextBlock>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
如您所见,我已经使用Itemssource = {Binding},这意味着Itemssource绑定到SelectedCustomer,因为在我后面的代码中已设置
this.datacontext = SelectedCustomer
然后在ItemTemplate和Content Template中,使用
将文本块和文本框绑定到SelectedCustomer的属性。{Binding <PropertyName>}