user2109254
user2109254

Reputation: 1759

XamDataGrid - How to create a DataRecordPresenterStyle DataTrigger from C# code

I am trying to do this:

<Style TargetType="{x:Type igDP:DataRecordPresenter}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding DataItem.IsOnChart}" Value="true">
            <Setter Property="Opacity" Value="1"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding DataItem.IsOnChart}" Value="false">
            <Setter Property="Opacity" Value="0.5"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

From C#:

Style _DataRecordPresenterStyle = new Style(typeof(DataRecordPresenter));
_DataRecordPresenterStyle.Setters.Add(new Setter(DataRecordPresenter.OpacityProperty, 1));

var _DataTrigger = new DataTrigger() { Binding = new Binding("DataItem.IsOnChart"), Value = true };
_DataTrigger.Setters.Add(new Setter(DataRecordPresenter.OpacityProperty, 1));
_DataRecordPresenterStyle.Triggers.Add(_DataTrigger);

_DataTrigger = new DataTrigger() { Binding = new Binding("DataItem.IsOnChart"), Value = false };
_DataTrigger.Setters.Add(new Setter(DataRecordPresenter.OpacityProperty, 0.5));
_DataRecordPresenterStyle.Triggers.Add(_DataTrigger);

_Grid.FieldLayoutSettings.DataRecordPresenterStyle = _DataRecordPresenterStyle;

But when I bind the data to the grid I get the error:

Default Unhandled exception: Exception has been thrown by the target of an invocation.

The data does have the field, it's type is a bool and the value is true on all records.

What am I doing wrong here?

Thanks for your time.

Upvotes: 0

Views: 614

Answers (1)

mm8
mm8

Reputation: 169240

"Exception has been thrown by the target of an invocation" doesn't say much. You should check the message of the InnerException. I also assume that you have verified that the Style itself works if you use it in your XAML markup.

The recommended way of creating a Style programmtically is to use the XamlReader class and parse XAML. Try this:

string xaml = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
    "xmlns:igDP=\"clr-namespace:Infragistics.Windows.DataPresenter;assembly=InfragisticsWPF4.DataPresenter.v12.1\" " +
    "TargetType=\"{x:Type igDP:DataRecordPresenter}\">" +
"<Style.Triggers>" +
        "<DataTrigger Binding=\"{Binding DataItem.IsOnChart}\" Value=\"true\">" +
            "<Setter Property=\"Opacity\" Value=\"1\"/>" +
        "</DataTrigger>" +
        "<DataTrigger Binding=\"{Binding DataItem.IsOnChart}\" Value=\"false\">" +
            "<Setter Property=\"Opacity\" Value=\"0.5\"/>" +
        "</DataTrigger>" +
    "</Style.Triggers>" +
"</Style>";

Style style = System.Windows.Markup.XamlReader.Parse(xaml) as Style;
style.Seal();

You may have to change "InfragisticsWPF4.DataPresenter.v12.1" to the actual name of the assembly in which the DataRecordPresenter class is defined.

Upvotes: 1

Related Questions