C# 如何更改选定项的DataGridColumn绑定?
假设我有一个绑定到对象集合的C# 如何更改选定项的DataGridColumn绑定?,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,假设我有一个绑定到对象集合的DataGrid。这些对象具有属性PropertyA和PropertyB。我希望第一列显示PropertyA,但当我选择一行时,我只希望所选行显示PropertyB。我该怎么做 对象 public class MyObject { public string PropertyA { get; set; } public string PropertyB { get; set; } } xaml <DataGrid ItemsSource="{Bindi
DataGrid
。这些对象具有属性PropertyA
和PropertyB
。我希望第一列显示PropertyA
,但当我选择一行时,我只希望所选行显示PropertyB
。我该怎么做
对象
public class MyObject
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
xaml
<DataGrid ItemsSource="{Binding Path=MyObjects}">
<DataGrid.Columns>
<DataGridTextColumn Header="Foo" Binding="{Binding Path=PropertyA}" />
</DataGrid.Columns>
</DataGrid>
这将在PropertyA中显示datagrid中每一行的值。但当我选择一行时,我只希望该行更改为显示属性b。尝试以下操作:
XAML:
Window x:Class="WpfApplication296.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication296"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="TemplateA">
<TextBlock Text="{Binding PropertyA}" FontSize="24" />
</DataTemplate>
<DataTemplate x:Key="TemplateB">
<TextBlock Text="{Binding PropertyB}" FontSize="24"/>
</DataTemplate>
<Style x:Key="DataGridCellStyle1"
TargetType="{x:Type DataGridCell}"
BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateA}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateB}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding MyObjects}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Foo"
Width="*"
Binding="{Binding PropertyA}"
CellStyle="{StaticResource DataGridCellStyle1}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
public class MyViewModel
{
public ObservableCollection<MyObject> MyObjects { get; set; }
public MyViewModel()
{
MyObjects = new ObservableCollection<MyObject>
{
new MyObject {PropertyA = " AAA 101", PropertyB=" BBBBBB 001" },
new MyObject {PropertyA = " AAA 102", PropertyB=" BBBBBB 002" },
new MyObject {PropertyA = " AAA 103", PropertyB=" BBBBBB 003" },
new MyObject {PropertyA = " AAA 104", PropertyB=" BBBBBB 004" },
new MyObject {PropertyA = " AAA 105", PropertyB=" BBBBBB 005" },
};
}
}
windowx:Class=“WpfApplication296.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local=“clr命名空间:WpfApplication296”
mc:Ignorable=“d”
Title=“MainWindow”Height=“300”Width=“300”>
视图模型:
Window x:Class="WpfApplication296.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication296"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="TemplateA">
<TextBlock Text="{Binding PropertyA}" FontSize="24" />
</DataTemplate>
<DataTemplate x:Key="TemplateB">
<TextBlock Text="{Binding PropertyB}" FontSize="24"/>
</DataTemplate>
<Style x:Key="DataGridCellStyle1"
TargetType="{x:Type DataGridCell}"
BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateA}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateB}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding MyObjects}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Foo"
Width="*"
Binding="{Binding PropertyA}"
CellStyle="{StaticResource DataGridCellStyle1}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
public class MyViewModel
{
public ObservableCollection<MyObject> MyObjects { get; set; }
public MyViewModel()
{
MyObjects = new ObservableCollection<MyObject>
{
new MyObject {PropertyA = " AAA 101", PropertyB=" BBBBBB 001" },
new MyObject {PropertyA = " AAA 102", PropertyB=" BBBBBB 002" },
new MyObject {PropertyA = " AAA 103", PropertyB=" BBBBBB 003" },
new MyObject {PropertyA = " AAA 104", PropertyB=" BBBBBB 004" },
new MyObject {PropertyA = " AAA 105", PropertyB=" BBBBBB 005" },
};
}
}
公共类MyViewModel
{
公共可观察集合MyObjects{get;set;}
公共MyViewModel()
{
MyObjects=新的可观察集合
{
新的MyObject{PropertyA=“AAA 101”,PropertyB=“BBBB 001”},
新的MyObject{PropertyA=“AAA 102”,PropertyB=“bbbb 002”},
新的MyObject{PropertyA=“AAA 103”,PropertyB=“BBBB 003”},
新的MyObject{PropertyA=“AAA 104”,PropertyB=“bbbb 004”},
新的MyObject{PropertyA=“AAA 105”,PropertyB=“BBBB 005”},
};
}
}