DataGrid
컨트롤을 사용하는 WPF 응용 프로그램이 있으며 경찰차에서 경찰이 사용합니다. 병합 된 사전을 사용하여 낮과 밤의 "모드"를 구현합니다.이 모드에서는 두 가지 프로그램간에 프로그램을 전환 할 때 색상 표가 변경됩니다. 이 응용 프로그램은 회사에서 만든 특수 센서의 데이터를 수집하여 담당자에게 표시합니다.DataGridRow의 배경 속성이 잘못되었습니다.
해당 DataGrid
은 이상하게 작동합니다. 프로그램을 처음 시작하면 처음에는 비어 있습니다. 데이터가 수집되면 행이 DataGrid
에 추가됩니다. 프로그램을 시작하면 처음에는 주간 모드입니다. 문제는 첫 번째 행의 배경이 컨트롤의 야간 모드 색으로 변경되지 않는다는 것입니다. 흰색으로 유지되며 낮 모드 색상입니다. 주간 모드와 야간 모드 사이를 앞뒤로 전환하면 흰색으로 유지됩니다.
그 다음에 DataGrid
에 추가 된 행은 올바른 색을 가지며 색상 사이를 적절히 전환합니다. 여기
DataGridRow
클래스 내 App.xaml에서 정의한 스타일이다 : 나는이 프로그램에 스눕을 실행하고 문제의
DataGridRow
로 드릴 다운 할 때
<Application x:Class="MyApplication.App"
. . .>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApplication;component/DayTime.xaml" />
<ResourceDictionary>
. . .
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="Foreground" Value="{DynamicResource DataForeground}" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorderFocused}" />
<Setter Property="Foreground" Value="{DynamicResource DataForeground}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorderFocused}" />
<Setter Property="Foreground" Value="{DynamicResource DataForeground}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource DataBackgroundSelected}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorderSelected}" />
<Setter Property="Foreground" Value="{DynamicResource DataForegroundSelected}" />
</Trigger>
</Style.Triggers>
</Style>
. . .
</ResourceDictionary>
</ResourceDictionary>
</ResourceDictionary>
</Application.Resources>
</Application>
의 Background
속성의 값은 흰색입니다 (#FFFFFFFF)이고 값 소스는 "DefaultStyle"로 설정됩니다. 이것은 내가 정의한 스타일이 아닌 것처럼 보이지만, 낮 모드로 전환 할 때 변경되지 않기 때문에 &은 흰색으로 되돌아갑니다. Microsoft에서 정의한 기본 스타일이고 내 스타일을 전혀 사용하지 않는 것 같습니다. 그러나 처음에 비어있는 경우에만 DataGrid
에 삽입 된 첫 번째 행에 있습니다.
후속 행의 경우 값 원본 열에 "ParentTemplate"이 표시됩니다. 야간 모드를 전환 할 때 배경색이 올바르게 변경되므로 내 스타일을 사용해야합니다.
DataGrid
의 모든 행이 올바른지 확인하려면 어떻게해야합니까?
는 편집 : 완성도의 이익에
는 여기에 도움이 경우, DataGrid
제어에 사용되는 스타일입니다. expermient으로
, 나는이 문제에 윈도우의 코드 숨김이 멤버 변수를 추가 : 편집
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="Foreground" Value="{DynamicResource TextForeground}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Command="{x:Static DataGrid.SelectAllCommand}"
Focusable="false"
Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<DataGridColumnHeadersPresenter Grid.Column="1"
x:Name="PART_ColumnHeadersPresenter"
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
CanContentScroll="{TemplateBinding CanContentScroll}"
Grid.ColumnSpan="2"
Grid.Row="1" />
<ScrollBar x:Name="PART_VerticalScrollBar"
Grid.Column="2"
Maximum="{TemplateBinding ScrollableHeight}"
Orientation="Vertical"
Grid.Row="1"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportHeight}"
MinWidth="45" Width="50" />
<Grid Grid.Column="1"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableWidth}"
Orientation="Horizontal"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportWidth}" />
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</Trigger>
</Style.Triggers>
</Style>
는
다음
private static Style dataGridRowStyle = null;
나는의 생성자에이 코드를 추가 내 창 :
if (dataGridRowStyle == null) {
dataGridRowStyle = FindResource(typeof(DataGridRow)) as Style;
MyGrid.RowStyle = dataGridRowStyle;
}
이렇게하면 모든 행이 DataGrid
d 원래의 기본 스타일. 위 코드를 Loaded
이벤트 처리기로 옮겼을 때도 마찬가지입니다.
다음으로 위 코드를 제거하고 x : Key 특성을 app.xaml 파일의 Style
정의에 추가했습니다. 나는 다음 DataGrid
제어의 정의에이 속성을 추가 :
RowStyle={DynamicResource MyDataGridRowStyle}
지금 모든 행은 내 스타일이있다.어떤 위대한,하지만 내 스타일을 선언하는 것이라고 생각했다
TargetType
속성을 모든 행에 적용 할 수 있습니다. 왜 안 그랬어?