Я использую WPF. NET Core 3.0. У меня есть сетка данных, и я хочу использовать стиль для установки полей строк в соответствии со значением свойства в исходном объекте. Я пытаюсь этот код:
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellStyle">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Margin" Value="-1,10,0,0"/>
<Setter Property="FontSize" Value="9"/>
<Setter Property="Height" Value="18"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}" >
<Border Padding="9,0,0,0">
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=MyPropertyInSource, RelativeSource={RelativeSource Self}}" Value="-1">
<Setter Property="Margin" Value="-1,-30,0,0"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataGrid Name="MyDataGrid"
ItemsSource="{Binding Data}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding MyPropertyTitle}" Width="29.7cm"
CellStyle="{StaticResource DataGridCellStyle}"/>
</DataGrid.Columns>
</DataGrid>
В стиле, в триггере данных, я пытаюсь связать свойство источника в и, если оно равно -1, установить верхнее поле на -30 , если нет, используйте поле по умолчанию, которое установлено со значением или 10. Я проверил, что исходный объект имеет значение -1 в свойстве, но триггер данных не генерируется.
Я хотел бы использовать стиль, потому что у меня есть некоторая сетка данных, которая использует тот же лог c, и я хотел бы повторно использовать стиль, если возможно избежать необходимости каждый раз копировать.
РЕДАКТИРОВАТЬ: I добавит больше кода с классами, которые используются в качестве источника данных.
<DataGrid Name="MainDataGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" Grid.Column="0" Grid.Row="0"
Style="{StaticResource DataGridMainStyle}"
RowStyle="{DynamicResource DataGridMainRowStyle}"
ItemsSource="{Binding Data}">
<DataGrid.Columns>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid Name="DetailsDataGrid"
Style="{StaticResource DataGridDetailsStyle}"
CellStyle="{StaticResource DataGridDetailsCellStyle}"
ItemsSource="{Binding MyCollectionDetails}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding DatailsProperty}" Width="29.7cm"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
<Style x:Key="DataGridMainStyle" TargetType="{x:Type DataGrid}">
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="VerticalGridLinesBrush" Value="Black"/>
<Setter Property="HorizontalGridLinesBrush" Value="Black"/>
</Style>
<Style x:Key="DataGridDetailsStyle" TargetType="{x:Type DataGrid}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellStyle">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Margin" Value="-1,10,0,0"/>
<Setter Property="FontSize" Value="9"/>
<Setter Property="Height" Value="18"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}" >
<Border Padding="9,0,0,0">
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=MyPropertyInSource, RelativeSource={RelativeSource Self}}" Value="-1">
<Setter Property="Margin" Value="-1,-30,0,0"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public class Datos : BaseViewModel
{
private ObservableCollection<Details> _details = new ObservableCollection<Details>();
public ObservableCollection<Details> Details
{
get { return _details; }
set
{
_details = value;
base.RaisePropertyChangedEvent(nameof(Details));
}
}
}
public class Details
{
private MyType _myPrerty;
public MyType MyPrerty
{
get { return _myPrerty; }
set
{
_myPrerty = value;
base.RaisePropertyChangedEvent(nameof(MyPrerty));
}
}
}
private ObservableCollection<Data> _data = new ObservableCollection<Data>();
public ObservableCollection<Datos> Data
{
get { return _data; }
set
{
_data = value;
base.RaisePropertyChangedEvent(nameof(Data));
}
}