I have the query and tried this sample but getting error: "Cannot find the type 'MyEditorStyleSelector'."
XAML:
<Window x:Class="WpfApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="415" Width="717" xmlns:igDP="http://infragistics.com/DataPresenter" xmlns:igc="clr-namespace:Infragistics.Windows.DataPresenter;assembly=Infragistics3.WPF.DataPresenter.v7.2" xmlns:igEditors="clr-namespace:Infragistics.Windows.Editors;assembly=Infragistics3.WPF.Editors.v7.2" xmlns:local="clr-namespace:WpfApplication2;assembly=WpfApplication2"> <Grid> <Grid.Resources> <Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="customFieldOperatorEditor"> <Setter Property="EditTemplate"> <Setter.Value> <ControlTemplate TargetType="{x:Type igEditors:XamTextEditor}"> <StackPanel> <!--will use custom ComboBox here--> <ComboBox Name ="cmbFieldOperator" SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay}" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="customFieldValueEditor"> <Setter Property="EditTemplate"> <Setter.Value> <ControlTemplate TargetType="{x:Type igEditors:XamTextEditor}"> <StackPanel> <!--will use custom TextBox here--> <TextBox Name ="txtFieldValue" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <igc:XamDataGrid x:Name="CriteriaGrid" Height="128" GroupByAreaLocation="None"> <igc:XamDataGrid.FieldLayoutSettings> <igc:FieldLayoutSettings AutoGenerateFields="False" AllowAddNew="True" AddNewRecordLocation="OnBottom" /> </igc:XamDataGrid.FieldLayoutSettings> <igc:XamDataGrid.FieldLayouts> <igc:FieldLayout> <igc:FieldLayout.Fields> <igc:Field Name="FieldName" Label ="Field Name" /> <igc:Field Name="FieldValue" Label="Field Value" > <igc:Field.Settings> <igc:FieldSettings EditorStyleSelector="{x:Static local:MyEditorStyleSelector.Instance}" /> </igc:Field.Settings> </igc:Field> <igc:Field Name="DataType" Visibility="Hidden" /> </igc:FieldLayout.Fields> </igc:FieldLayout> </igc:XamDataGrid.FieldLayouts> </igc:XamDataGrid> </Grid></Window>
XAML code behind:
using Infragistics.Windows.DataPresenter;using Infragistics.Windows.DataPresenter.Events;namespace WpfApplication2{ public class MyEditorStyleSelector : StyleSelector { public static readonly MyEditorStyleSelector Instance = new MyEditorStyleSelector();
public override Style SelectStyle(object item, DependencyObject container) { //business logic will go here to return the Style //// something similar to your code } } public partial class Window1 : Window { public Window1() { InitializeComponent(); CriteriaGrid.DataSource = DataBind(); } }}
Again, I am getting error: "Cannot find the type 'MyEditorStyleSelector'." Let me know if I need to attach the sample code. Please assist.
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?
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.
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