选中时更改网格行的背景颜色
Posted
技术标签:
【中文标题】选中时更改网格行的背景颜色【英文标题】:Change Background color of grid row when selected 【发布时间】:2015-05-11 22:57:57 【问题描述】:我的itemscontrol
当前正在正确加载每一行。我试图让它在用户选择它时更改每行的背景颜色。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0">
<Border BorderBrush="#BBBDBF" Background="#F4F4F4" BorderThickness="0,1,0,1" Margin="10,10,10,10"/>
<Image HorizontalAlignment="Left" Margin="10,0,0,0" Height="38" Width="38" Source="C:\Users\linda_l\Pictures\Cloud upload\database (1).png" />
<TextBlock FontSize="50" Foreground="#4092C2" Margin="60,0,0,0" HorizontalAlignment="left" Height="69" >Databases</TextBlock>
</Grid>
<Grid Background="White" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="10" />
</Grid.ColumnDefinitions>
<Border BorderBrush="#BBBDBF" BorderThickness="0,0,0,1" Grid.Column="1" />
<Label Grid.Column="1" Content="Server Name" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="#4092C2" />
<Border BorderBrush="#BBBDBF" BorderThickness="1,0,0,1" Grid.Column="2" />
<Label Grid.Column="2" Content="Source Database" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="#4092C2" />
<Border BorderBrush="#BBBDBF" BorderThickness="1,0,0,1" Grid.Column="3" />
<Label Grid.Column="3" Content="Destination Database" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="#4092C2" />
<Border BorderBrush="#BBBDBF" BorderThickness="1,0,0,1" Grid.Column="4" />
<Label Grid.Column="4" Content="Status" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="#4092C2" />
</Grid>
<ItemsControl x:Name="itemscntrl" ItemsSource="Binding DatabaseServers" Background="White" BorderBrush="WhiteSmoke" BorderThickness="0" Grid.Row="2" Margin="0,5,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="grd" Background="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="10" />
</Grid.ColumnDefinitions>
<Image Source="Binding StatusImage" Height="10" Width="10" />
<Label Grid.Column="1" HorizontalAlignment="Left" Content="Binding ServerName" />
<Label Grid.Column="2" HorizontalAlignment="Left" Content="Binding SourceDatabase" />
<TextBox x:Name="dst" Grid.Column="3" Text="Binding DestinationDatabase , Mode=TwoWay, UpdateSourceTrigger=LostFocus" VerticalAlignment="Top" />
<Label Grid.Column="4" Content="Binding Status" VerticalAlignment="Top" />
<Button Grid.Column="4" BorderThickness="0" HorizontalAlignment="Center" Width="50" Margin="3" Content="Binding Status" Command="Binding EnabledCommand" CommandParameter="Binding" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="Binding selected " Value="True">
<Setter Property="Background" Value="Yellow" TargetName="dst" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
目前看起来像这样:
我尝试添加一个DataTemplate.Triggers
,但它没有接缝做任何事情。您如何准确地检测到项目控件中的一行已被选中?我发现的唯一示例使用datagrid
。我尝试将我的更改为datagrid
而不是itemscontrol
,但是它不会加载数据。
数据网格:
<DataGrid x:Name="grd" Background="White" DataContext="Binding DatabaseServers" Grid.Row="2">
<Image Source="Binding StatusImage" Height="10" Width="10" />
<Label Grid.Column="1" HorizontalAlignment="Left" Content="Binding ServerName" />
<Label Grid.Column="2" HorizontalAlignment="Left" Content="Binding SourceDatabase" />
<TextBox x:Name="dst" Grid.Column="3" Text="Binding DestinationDatabase , Mode=TwoWay, UpdateSourceTrigger=LostFocus" VerticalAlignment="Top" />
<Label Grid.Column="4" Content="Binding Status" VerticalAlignment="Top" />
<Button Grid.Column="4" BorderThickness="0" HorizontalAlignment="Center" Width="50" Margin="3" Content="Binding Status" Command="Binding EnabledCommand" CommandParameter="Binding" />
</DataGrid>
只显示一堆行,每行没有数据。
我对 WPF 很陌生,所以我真的不知道我在这里做错了什么。
【问题讨论】:
@SriramSakthivel 我没有使用数据网格我使用的是网格,所以这没有帮助。一个小时前我也试过了:) 我的错误。重新打开问题。 为什么不使用数据网格并根据需要自定义所选行 我尝试将其更改为数据网格,但我的数据无法加载。 ItemControls 没有 Selected Items,您需要使用 ListBox 或 ListView。然后将样式应用到 ListBoxItem。 【参考方案1】:这是你想要的使用DataGrid
我使用MVVM
Window.xaml
<Grid Margin="10">
<StackPanel Orientation="Vertical">
<Label Content="DataBases" Width="150" Height="50" HorizontalAlignment="Left" FontSize="20"/>
<DataGrid Name="DgDataSource" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="Binding SourceData">
<DataGrid.Columns>
<DataGridTextColumn Header="ServerName" Binding="Binding ServerName" Width="2*"/>
<DataGridTextColumn Header="SourceDatabase" Binding="Binding SourceDatabase" Width="2*"/>
<DataGridTextColumn Header="DestinationDatabase" Binding="Binding DestinationDatabase" Width="2*"/>
<DataGridTemplateColumn Width="*" Header="Status" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Binding Status" Command="Binding EnabledCommand"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
查看模型
public class WindowViewModel
public ObservableCollection<DataSource> SourceData get; set;
public WindowViewModel()
Initialize();
private void Initialize()
SourceData = new ObservableCollection<DataSource>
new DataSource() Status = "Stop", ServerName = "Test 1", SourceDatabase = "Unknown",DestinationDatabase = "blabla.....",
new DataSource() Status = "Work", ServerName = "Test 2", SourceDatabase = "Unknown",DestinationDatabase = "blabla.....",
new DataSource() Status = "Stop", ServerName = "Test 3", SourceDatabase = "Unknown",DestinationDatabase = "blabla....."
;
模型
public class DataSource
public string Status get; set;
public string ServerName get; set;
public string SourceDatabase get; set;
public string DestinationDatabase get; set;
【讨论】:
哇,你得到的不仅仅是一个 1 和一个,除了把它们放在一起。它和我想出的差不多,我也找到了 DataGridTemplateColumn :) hhh y re 欢迎兄弟...好吧,然后告诉你的朋友给我一些投票:p 对于命令你最好添加一个中继命令如果你需要如何修复它告诉我以上是关于选中时更改网格行的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章
MPMediaPickerController 图标的背景颜色,未选中时