Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
215
ComboBox for some rows?
posted
Hi, im trying to do something like this... Basically I want to display a ComboBox if the current row meets some condition (Record.DataItem.Inherited), otherwise display a standard textBox.. Is this possible? if so, my current xaml isnt working, any ideas why? Thanks. EDIT: code added to next post.. sorry it looks so nasty..
Parents
  • 6867
    posted

    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

Reply
  • 3806
    Offline posted in reply to [Infragistics] Josh Smith

    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.

Children
No Data