wpf基于行项值的datagrid单元格样式

时间:2017-03-02 14:07:53

标签: c# wpf xaml datagrid

这应该相对容易,但由于某种原因,我没有让它工作。我的课程ViewWrapper有一个属性ViewGroupViewGroupEditable。我的数据网格中有一个绑定到ViewGroup属性的列,现在我想在属性ViewGroupEditable为false时禁用该单元格上的编辑。所以我创建了一个带有简单数据触发器的DataGridCell样式,但它没有做任何事情。我错过了什么?

XAML:

<UserControl x:Class="GrimshawRibbon.Revit.Views.ViewManager.ViewManagerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:local="clr-namespace:GrimshawRibbon.Revit.Views.ViewManager"
             xmlns:ex="clr-namespace:GrimshawRibbon.Revit.Wpf.Extensions"
             xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="500">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                <ResourceDictionary Source="pack://application:,,,/GrimshawRibbon;component/Revit/Wpf/Style/GrimshawTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.Resources>
        <Style x:Key="dgCellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ViewGroupEditable}" Value="True">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Setter Property="Foreground" Value="LightGray"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        </Grid.Resources>
        <ex:DataGridEx x:Name="dgViews" 
                               Style="{StaticResource AzureDataGrid}" 
                               Margin="10" 
                               AutoGenerateColumns="False" 
                               ItemsSource="{Binding Views, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                               CanUserAddRows="False" 
                               IsReadOnly="False" 
                               SelectionMode="Extended" 
                               SelectionUnit="FullRow" 
                               SelectedItem="{Binding SelectedRow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                               SelectedItemsList="{Binding SelectedViews, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="CellEditEnding">
                    <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding CellEditEndingCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
                <DataGridTextColumn Header="ViewType" Binding="{Binding ViewType}" Width="100" IsReadOnly="True"/>
                <DataGridTextColumn Header="ViewGroup" Binding="{Binding ViewGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="130" CellStyle="{StaticResource dgCellStyle}"/>
                <DataGridTextColumn Header="ViewSubGroup" Binding="{Binding ViewSubGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="130" IsReadOnly="False"/>
                <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                        EditingElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                        Header="OnSheet" 
                                        Binding="{Binding OnSheet, Mode=TwoWay}" 
                                        IsReadOnly="True" 
                                        Width="80">
                </DataGridCheckBoxColumn>
                <DataGridTemplateColumn Header="ViewTemplate" Width="180">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ViewTemplate.Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                      SelectedItem="{Binding ViewTemplate}"
                                      DisplayMemberPath="Name">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <cmd:EventToCommand PassEventArgsToCommand="True"
                                                            Command="{Binding DataContext.SelectChangeCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </ex:DataGridEx>
    </Grid>
</UserControl>

数据网格中的每一行都绑定到这样的对象:

public class ViewWrapper : ElementWrapper
    {
        public string ViewType { get; set; }
        public bool IsSelected { get; set; }
        public bool OnSheet { get; set; }
        public string ViewGroup { get; set; }
        public string ViewSubGroup { get; set; }
        public ViewWrapper ViewTemplate { get; set; }
        public bool ViewGroupEditable { get; set; }
        public bool ViewSubGroupEditable { get; set; }
    }

2 个答案:

答案 0 :(得分:1)

我能看到的唯一问题是String content; Pattern pattern = Pattern.compile(".*(?<=overlay-link\" href=\").*?(?=\">).*"); try { Scanner scanner = new Scanner(new File("sourceCode.txt")); while (scanner.hasNext()) { content = scanner.nextLine(); if (pattern.matcher(content).matches()) { System.out.println(content.replaceAll("(?<=overlay-link\" href=\").*?(?=\">)", "")); } } } catch (IOException ex) { Logger.getLogger(SourceCodeExample.class.getName()).log(Level.SEVERE, null, ex); } 没有实现ViewWrapper。您应该对所有属性使用类似的内容:

INotifyPropertyChanged

请参阅this post以及其他许多内容。

答案 1 :(得分:0)

DataGridTextColumn没有IsEnabled属性,因此无法设置。您可以尝试使用IsReadOnly(请注意,您将禁用整个列)。

这种方法可能会更幸运:

 <DataGridTextColumn Width="80" Header="My column header" Binding="{Binding Path=MyProperty, Mode=TwoWay}">
    <DataGridTextColumn.EditingElementStyle>
       <Style TargetType="TextBox">
           <Setter Property="IsEnabled" Value="{Binding IsEditingEnabled}"/>
       </Style>
    </DataGridTextColumn.EditingElementStyle>
  </DataGridTextColumn>

您可以为列中显示的数据定义两种样式:普通样式(ElementStyle)以及在编辑模式(EditingElementStyle)中显示的样式。虽然您可能希望在这种情况下进行更多样式设置,以使TextBlockTextBox具有一致的外观。

或者这种方式(未经测试):

 <Style x:Key="dgCellStyle" TargetType="{x:Type TextBox}"">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewGroupEditable}" Value="True">
                <Setter Property="IsEnabled" Value="False"/>
                <Setter Property="Foreground" Value="LightGray"/>
            </DataTrigger>
        </Style.Triggers>
  </Style>

  <DataGridTextColumn Header="ViewGroup" 
       Binding="{Binding ViewGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       Width="130" 
       EditingElementStyle="{StaticResource dgCellStyle}"/>