Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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
C# 在绑定属性上设置WPF DataGrid行的样式_C#_Wpf_Xaml_Data Binding_Datagrid - Fatal编程技术网

C# 在绑定属性上设置WPF DataGrid行的样式

C# 在绑定属性上设置WPF DataGrid行的样式,c#,wpf,xaml,data-binding,datagrid,C#,Wpf,Xaml,Data Binding,Datagrid,我有一个绑定到EventRecords列表的数据网格。EventRecord对象上有一个属性IsAutoEvent。我正试图根据此属性是否为真来设置行的背景色样式,但DataContext并没有像我预期的那样设置 <DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent

我有一个绑定到EventRecords列表的数据网格。EventRecord对象上有一个属性
IsAutoEvent
。我正试图根据此属性是否为真来设置行的背景色样式,但DataContext并没有像我预期的那样设置

<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsAutoEvent}">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

目前,这没有任何作用。我不认为datacontext是正确的,因为在
DataTrigger
下,
{Binding}
显示视图的属性(例如MainViewModel),而不是DataGridRow(例如EventRecord),正如我所料


有什么想法吗?

您没有将
放在
数据触发器上

               <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

您没有将
置于
DataTrigger

               <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

请将
值添加到
数据触发器中

               <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

请将
值添加到
数据触发器中

               <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>


是的,完全正确。根据提交的时间,我接受了@nit的答案。但不管怎样,对你来说+1!是的,完全正确。根据提交的时间,我接受了@nit的答案。但不管怎样,对你来说+1!