在DataTrigger的Setter中绑定不起作用

时间:2018-04-25 13:13:41

标签: c# wpf wpfdatagrid

在我的数据网格中,我以编程方式添加列,以便我可以定义它们的顺序 如果显示的项目(MediaRow)包含由其属性HasError指示的错误,则会隐藏其他一些列,并希望StatusColumn使用现在为空的空格。

为此,我使用绑定到HasError的DataTrigger。 在那个触发器中,我设置了保证金,但这并没有按预期工作。

如果我使用固定值,它就可以工作。

dt.Setters.Add(new Setter(DataGridCell.MarginProperty, new Thickness(-150,0,0,0))

但如果我使用绑定它就不会。

dt.Setters.Add(new Setter(DataGridCell.MarginProperty, new Binding() { Path = new PropertyPath("StatusColumnMargin"), Mode = BindingMode.OneWay}));
  

我是否需要将绑定应用于另一个StatusColumnMargin   方式还是这是正确的方法?

class Blibla : VMBase
{
    private Thickness _statusColumnMargin;
    public Thickness StatusColumnMargin
    {
        get => _statusColumnMargin;
        set
        {
            _statusColumnMargin = value;
            RaisePropertyChanged();
        }
    }

    private ObservableCollection<DataGridColumn> _columns;
    public ObservableCollection<DataGridColumn> ColumnCollection
    {
        get { return _columns; }
        set
        {
            if (_columns != value)
            {
                _columns = value;
                RaisePropertyChanged();
            }
        }
    }


    private ObservableCollection<MediaRow> mediaRowCollection;
    public ObservableCollection<MediaRow> MediaRowCollection
    {
        get
        {
            return mediaRowCollection;
        }
        set
        {
            mediaRowCollection = value;
            RaisePropertyChanged();
        }
    }


    private void PopulateColumns()
    {
        if (StatusColumnMargin == null)
            StatusColumnMargin = new Thickness(-150, 0, 0, 0);

        ObservableCollection<DataGridColumn> _loccolumns = new ObservableCollection<DataGridColumn>();

        DataGridTextColumn medgrpcolumn = new DataGridTextColumn();
        medgrpcolumn.Header = ResourceManagerService.GetResourceString("EasyTerminalClient_NG", "ColumnMediaGroup");
        medgrpcolumn.Width = new DataGridLength(Settings.Instance.CheckinColumnTable["mediagroup"], DataGridLengthUnitType.Star);
        medgrpcolumn.Binding = new Binding { Path = new PropertyPath("MedGrp"), Mode = BindingMode.TwoWay };
        medgrpcolumn.Visibility = Visibility.Hidden;
        medgrpcolumn.HeaderStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
        medgrpcolumn.HeaderStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
        medgrpcolumn.HeaderStyle.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Transparent));
        medgrpcolumn.HeaderStyle.Setters.Add(new Setter(Control.FontSizeProperty, new Binding { Path = new PropertyPath("headerSize"), Source = (EasyCheck.EasyTerminalClient_NG.Util.FontDetails)App.Current.FindResource("FontDetails") }));
        medgrpcolumn.CellStyle = new Style(typeof(DataGridCell));
        medgrpcolumn.CellStyle.Setters.Add(new Setter(DataGridCell.VisibilityProperty, new Binding() { Path = new PropertyPath("HasError"), Mode = BindingMode.OneWay, Converter = new InverseBool2VisibilityConverter() }));

        DataGridTextColumn duedatecolumn = new DataGridTextColumn();
        duedatecolumn.Header = ResourceManagerService.GetResourceString("EasyTerminalClient_NG", "ColumnExpires");
        //duedatecolumn.Width = new DataGridLength(Settings.Instance.CheckinColumnTable["expires"], DataGridLengthUnitType.Star);
        duedatecolumn.Binding = new Binding { Path = new PropertyPath("DueDate"), Mode = BindingMode.TwoWay };
        duedatecolumn.Visibility = Visibility.Hidden;
        duedatecolumn.HeaderStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
        duedatecolumn.HeaderStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
        duedatecolumn.HeaderStyle.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Transparent));
        duedatecolumn.HeaderStyle.Setters.Add(new Setter(Control.FontSizeProperty, new Binding { Path = new PropertyPath("headerSize"), Source = (EasyCheck.EasyTerminalClient_NG.Util.FontDetails)App.Current.FindResource("FontDetails") }));
        duedatecolumn.CellStyle = new Style(typeof(DataGridCell));
        duedatecolumn.CellStyle.Setters.Add(new Setter(DataGridCell.VisibilityProperty, new Binding() { Path = new PropertyPath("HasError"), Mode = BindingMode.OneWay, Converter = new InverseBool2VisibilityConverter() }));

        DataGridTextColumn statuscolumn = new DataGridTextColumn();
        statuscolumn.Header = ResourceManagerService.GetResourceString("EasyTerminalClient_NG", "ColumnState");
        statuscolumn.Width = new DataGridLength(Settings.Instance.CheckinColumnTable["state"], DataGridLengthUnitType.Star);
        statuscolumn.Binding = new Binding() { Path = new PropertyPath("Status"), Mode = BindingMode.OneWay, Converter = new LanguageConverter(), FallbackValue = "???" };
        statuscolumn.Visibility = Visibility.Hidden;
        statuscolumn.HeaderStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
        statuscolumn.HeaderStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
        statuscolumn.HeaderStyle.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Transparent));
        statuscolumn.HeaderStyle.Setters.Add(new Setter(Control.FontSizeProperty, new Binding { Path = new PropertyPath("headerSize"), Source = (EasyCheck.EasyTerminalClient_NG.Util.FontDetails)App.Current.FindResource("FontDetails") }));

        DataTrigger dt = new DataTrigger()
        {
            Binding = new Binding() { Path = new PropertyPath("HasError"), Mode = BindingMode.OneWay },
            Value = "True"
        };
        dt.Setters.Add(new Setter(DataGridCell.MarginProperty, new Binding() { Path = new PropertyPath("StatusColumnMargin"), Mode = BindingMode.OneWay}));
        //dt.Setters.Add(new Setter(DataGridCell.MarginProperty, new Thickness(-150,0,0,0)));
        Style cellStyle = new Style(typeof(DataGridCell));
        cellStyle.Triggers.Add(dt);
        statuscolumn.CellStyle = cellStyle;

        DataGridBoundTemplateColumn imagestatecolumn = new DataGridBoundTemplateColumn();
        imagestatecolumn.Width = new DataGridLength(Settings.Instance.CheckinColumnTable["imagestate"], DataGridLengthUnitType.Star);
        imagestatecolumn.Visibility = Visibility.Hidden;
        imagestatecolumn.HeaderStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
        imagestatecolumn.HeaderStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Center));
        imagestatecolumn.HeaderStyle.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Transparent));
        imagestatecolumn.HeaderStyle.Setters.Add(new Setter(Control.FontSizeProperty, new Binding { Path = new PropertyPath("headerSize"), Source = (EasyCheck.EasyTerminalClient_NG.Util.FontDetails)App.Current.FindResource("FontDetails") }));
        imagestatecolumn.Binding = new Binding { Path = new PropertyPath("StatusImage"), Mode = BindingMode.TwoWay }; 
        imagestatecolumn.CellTemplateSelector = App.Current.FindResource("mySelector") as DataTemplateSelector;

        DataGridTextColumn bincolumn = new DataGridTextColumn();
        bincolumn.Header = ResourceManagerService.GetResourceString("EasyTerminalClient_NG", "ColumnBin");
        bincolumn.Width = new DataGridLength(Settings.Instance.CheckinColumnTable["bin"], DataGridLengthUnitType.Star);
        bincolumn.Binding = new Binding { Path = new PropertyPath("Bin"), Mode = BindingMode.TwoWay };
        bincolumn.Visibility = Visibility.Hidden;
        bincolumn.HeaderStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
        bincolumn.HeaderStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
        bincolumn.HeaderStyle.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Transparent));
        bincolumn.HeaderStyle.Setters.Add(new Setter(Control.FontSizeProperty, new Binding { Path = new PropertyPath("headerSize"), Source = (EasyCheck.EasyTerminalClient_NG.Util.FontDetails)App.Current.FindResource("FontDetails") }));
        bincolumn.CellStyle = new Style(typeof(DataGridCell));
        bincolumn.CellStyle.Setters.Add(new Setter(DataGridCell.VisibilityProperty, new Binding() { Path = new PropertyPath("HasError"), Mode = BindingMode.OneWay, Converter = new InverseBool2VisibilityConverter() }));
        bincolumn.CellStyle.Setters.Add(new Setter(DataGridCell.VerticalContentAlignmentProperty, VerticalAlignment.Center));

        if (Settings.Instance.CheckinColumnSequence != null)
        {
            //string columnnames = "number|barcode|title|mediagroup|branch|expires|fee|state|imagestate|";
            string[] columnSequence = Settings.Instance.CheckinColumnSequence;
            for (int i = 0; i < columnSequence.Length; i++)
            {
                DataGridColumn thiscolumn = null;
                switch (columnSequence[i])
                {
                    case "mediagroup":
                        thiscolumn = medgrpcolumn;
                        break;
                    case "expires":
                        thiscolumn = duedatecolumn;
                        break;
                    case "state":
                        thiscolumn = statuscolumn;
                        break;
                }
                if (thiscolumn != null)
                {
                    thiscolumn.Visibility = Visibility.Visible;
                    thiscolumn.DisplayIndex = i;
                    _loccolumns.Add(thiscolumn);
                }
            }

            ColumnCollection = _loccolumns;
        }

        // doesn't have any effect
        StatusColumnMargin = new Thickness(-300, 0, 0, 0);
    }
}



<DataGrid x:Name="dataGrid1"
        Margin="10,10,0,0"
        AutoGenerateColumns="False"
        c:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection, UpdateSourceTrigger=PropertyChanged}"
        ItemsSource="{Binding MediaRowCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
        MinRowHeight="26"
        Width="952"
        IsReadOnly="True"
        SelectionMode="Single"
        CanUserReorderColumns="False"
        CanUserResizeColumns="False"
        CanUserSortColumns="False"
        CanUserResizeRows="False"
        HorizontalScrollBarVisibility="Disabled"
        AreRowDetailsFrozen="True"
        HeadersVisibility="Column" VerticalScrollBarVisibility="Auto" >
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding InField}" Value="False">
                    <Setter Property="Background" Value="Transparent"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding InField}" Value="True">
                    <Setter Property="Background" Value="#ffffffac"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

1 个答案:

答案 0 :(得分:1)

尝试将Source的{​​{1}}设置为定义了要绑定的Binding属性的对象:

StatusColumnMargin