Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何根据源对象中的值设置样式中的边距?_Wpf_Datagrid - Fatal编程技术网

Wpf 如何根据源对象中的值设置样式中的边距?

Wpf 如何根据源对象中的值设置样式中的边距?,wpf,datagrid,Wpf,Datagrid,我正在使用WPF.NETCore3.0。我有一个datagrid,我想使用样式根据源对象中属性的值设置行的边距。我正在尝试以下代码: <Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellStyle"> <Setter Property="Background" Value="LightGray"/> <Setter Property="Margin" Value="-1,10,0,

我正在使用WPF.NETCore3.0。我有一个datagrid,我想使用样式根据源对象中属性的值设置行的边距。我正在尝试以下代码:

<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值,但是没有抛出数据触发器

我想使用一种样式,因为我有一些使用相同逻辑的数据网格,如果可以避免每次都要复制的话,我想重用该样式

编辑:我将添加更多用作数据源的类的代码

<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));
    }
}
从触发器中删除RelativeSource={RelativeSource Self},因为默认情况下,单元格的DataContext是ItemsSource中的当前项:

<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}" Value="-1">
            <Setter Property="Margin" Value="-1,-30,0,0"/>
        </DataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

谢谢我试过这么做,但没用。但是,如果我在视图模型中设置了一个属性,并将相对源设置为AncestorType,并且我可以访问datacontext的属性,那么它就可以工作了。因此,至少我认为这是可能的。@阿尔瓦罗加西亚:那么MyPropertySource在哪里定义?我用更多的代码编辑了原始文章,将类的代码用作数据源和xaml。@阿尔瓦罗加西亚:问题仍然存在;您试图绑定到的MyPropertySource属性在哪里定义?什么是MyPropertyTitle?请使用真名和代码段。