개체 컬렉션에 바인딩 된 DataGrid가 있습니다.WPF DataGrid에서 일부 셀의 배경색을 변경하는 방법?
개체의 속성 중 하나는 색값을 저장하는 문자열입니다. 이 "COLORBACKGROUND"휴대 클릭에
은 ColorPicker의 그것을 변경 열립니다.
내가 필요한 것은 DataGrid 행 (#RGB)에 표시된 값으로 셀의 배경색을 변경하는 것입니다.
<DataGrid SelectionUnit="Cell" SelectedCellsChanged="DgDataTable_OnSelectedCellsChanged" x:Name="DgDataTable" Grid.Row="0" Grid.ColumnSpan="2" Margin="10,20,10,0" AutoGenerateColumns="true" HeadersVisibility="All" RowHeaderWidth="20" Style="{StaticResource AzureDataGrid}" GridLinesVisibility="Horizontal" LoadingRow="dgDataTable_LoadingRow" ColumnHeaderHeight="10" AlternatingRowBackground="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" AutoGeneratingColumn="DgDataTable_AutoGeneratingColumn">
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="FontSize" Value="10"/>
<Setter Property="Background" Value="LightCyan"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.RowHeaderStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
<!-- <Style.Triggers>
<Trigger Property="Text" Value="John">
<Setter Property="Background" Value="LightGreen"/>
</Trigger>
</Style.Triggers> -->
</Style>
</DataGrid.CellStyle>
</DataGrid>
난을 자동 생성 열이 뭔가 노력이
Item.ColorBackground
이 컴파일되지 않습니다
private void DgDataTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "SrcAlert")
{
DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
e.Column = cb;
cb.ItemsSource = new List<string> {"1", "2"};
cb.SelectedValueBinding = new Binding("SrcAlert");
e.Column.Header = "SrcAlert";
}
if (e.PropertyName.Equals("ColorBackground"))
{
DataGridTextColumn tc = new DataGridTextColumn();
e.Column = tc;
tc.Foreground = (Color)ColorConverter.ConvertFromString(DgDataTable.CurrentCell.Item.ColorBackground);
}
}
... 나는 내 설명을 넣어, 내가 필요한 이잖아.
나는 내가 찾은 또 다른 솔루션을 시도 :if (e.PropertyName.Equals("ColorBackground"))
{
string s = DgDataTable.CurrentCell.Item.ToString();
e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, (Color)ColorConverter.ConvertFromString(s)));
}
을하지만 그건 실패했다.
도움 주셔서 감사합니다.
편집 : 나를 위해 완벽하게 작동 애쉬 솔루션의 스크린 샷 :
편집 :
: 나는 색상 선택기로 여러 열에 대한 솔루션을 적용색상 만 표시하는 스타일 설정 도구를 추가합니다. 셀에 :
셀을 클릭<Style TargetType="DataGridCell" x:Key="ColorPickerCellBG"
BasedOn="{StaticResource CommonCell}">
<Setter Property="Background" Value="{Binding Path=BG}"/>
<Setter Property="Foreground" Value="Transparent"/>
<Setter Property="Width" Value="30"></Setter>
</Style>
<Style TargetType="DataGridCell" x:Key="ColorPickerCellAL"
BasedOn="{StaticResource CommonCell}">
<Setter Property="Background" Value="{Binding Path=AL}"/>
<Setter Property="Foreground" Value="Transparent"/>
<Setter Property="Width" Value="30"></Setter>
</Style>
<Style...
는, RGB 색상 값이 표시되고 스타일이 "ClickedCell"해야 ... 어떻게 그것을 향상시킬 수 있습니까?
내가 필요한 것은 <의 의미가의 배경색을 변경하는 것입니다 셀을 DataGrid 행 (#RGB)에 표시된 값으로 변경합니다. > – AnjumSKhan
"# 556677"을 셀에 표시하는 대신 배경색을이 셀로 지정하십시오. – Alexus