I suggest you use the FieldSettings property called EditorStyleSelector. The TargetType of the Style it outputs will be used as the type of editor to display in the cell to which it is applied. For example, suppose I have these two styles in the XamDataGrid's Resources:
<igDP:XamDataGrid.Resources> <Style x:Key="ComboFieldStyle" TargetType="{x:Type igEditors:XamComboEditor}"> <Setter Property="ItemsProvider"> <Setter.Value> <igEditors:ComboBoxItemsProvider ItemsSource="{Binding Source={StaticResource MyXmlData}, XPath=/items/item}" DisplayMemberPath="@text" /> </Setter.Value> </Setter> </Style> <Style x:Key="TextFieldStyle" TargetType="{x:Type igEditors:XamTextEditor}"> <Setter Property="Margin" Value="2,0" /> </Style></igDP:XamDataGrid.Resources>
Then I make a StyleSelector, like so:
public class MyEditorStyleSelector : StyleSelector{ public static readonly MyEditorStyleSelector Instance = new MyEditorStyleSelector(); public override Style SelectStyle(object item, DependencyObject container) { CellValuePresenter cvp = container as CellValuePresenter; if (cvp == null) return null; Foo f = cvp.Record.DataItem as Foo; if (f == null) return null; if(f.Status == MyEnum.Value1) return cvp.TryFindResource("ComboFieldStyle") as Style; return cvp.TryFindResource("TextFieldStyle") as Style; }}
I can use that selector in the XamDataGrid's Field declarations:
<igDP:FieldLayout> <igDP:FieldLayout.Fields> <igDP:Field Name="Name" /> <igDP:Field Name="Status"> <igDP:Field.Settings> <igDP:FieldSettings EditorStyleSelector="{x:Static local:MyEditorStyleSelector.Instance}" /> </igDP:Field.Settings> </igDP:Field> </igDP:FieldLayout.Fields></igDP:FieldLayout>
I hope that helps,
Josh
If FieldSettings had Triggers or a Style property, then this could be done all in XAML. The amount of imperative code required to make this work, however, is minimal.
Hi,
I have same kind of problem. I have 2 styles based on cellvaluepresenter 1 displays button and other displays label(or anything I can have)
I am trying to bind the style at the runtime based on condition
If currentRecord.Cells("Field1").Value = "Image" Or currentRecord.Cells("Field1").Value = "System.Binary" Then
Else
End If
When I add a new record in a grid, it displays proper styles but when i reload the page it applies last applied style to the entire column. I dont know why?