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
1715
Binding to a complex property and editing it using XamComboEditor
posted

One of my properties is of a type called Country. The country has a ShortName : String and Name : String. My XamComboBox contains items of type ComboBoxDataItem with value of type Country and display path of type String (corresponding to a country's  Name). The idea is that I will select a Name from the drop down, and the proper Country will be set. However, I'm finding this quite difficult to do. My XAML looks like the following:

          <igDP:UnboundField Name="Country" Label="Country" BindingPath="Country.Name" BindingMode="TwoWay">
              <igDP:Field.Settings>
                <igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}" 
                                    AllowEdit="True" >
                  <igDP:FieldSettings.EditorStyle>
                    <Style TargetType="{x:Type igEditors:XamComboEditor}">
                      <Setter Property="ItemsProvider" Value="{StaticResource countriesP}" />
                    </Style>
                  </igDP:FieldSettings.EditorStyle>
                </igDP:FieldSettings>
              </igDP:Field.Settings>
            </igDP:UnboundField>

I'm displaying the country's name, which is all good. But I'd like to modify the Country property of my main Data Item. The XAML above certainly won't work, since the Binding will try to assign the Country object to country name. 

How do I do this? One way, which is nasty, it to have some kind of property of my DataItem that can be set and will easily assign the proper Country object to it, but is there a clearned way to view/edit complex properties?

 

Thanks,

-Szymon

Parents
No Data
Reply
  • 2070
    posted

    Hi Szymon,

     

    Why are you adding an unbound field. Why not simply just use the Country bound field directly. With this there will be no need to specify BindingPath. You can use XamComboEditor to directly edit field of a custom data type, in your case Country type.

     

    The following is what I just tried out with a sample project. It has a DataItem class which has a Country property of type Country. Both of these are defined below. XamComboEditor is used to edit the Country field directly and it all works. The style for the XamComboEditor is in code below as well. XAML portion simply defines a XamDataGrid and doesn't have any other code.

     

    public void OnLoaded( object sender, EventArgs e )
    {
        List<DataItem> dataSource = new List<DataItem>( );
        DataItem item;

        item = new DataItem( );
        item.Country = new Country( "a", "AA" );
        dataSource.Add( item );

        item = new DataItem( );
        item.Country = new Country( "b", "BB" );
        dataSource.Add( item );

        _dp.DataSource = dataSource;

        ComboBoxItemsProvider countriesIP = new ComboBoxItemsProvider( );
        countriesIP.Items.Add( new Country( "a", "AA" ) );
        countriesIP.Items.Add( new Country( "b", "BB" ) );
        countriesIP.DisplayMemberPath = "Name";

        FieldSettings settings = _dp.FieldLayouts[0].Fields["Country"].Settings;
        settings.EditorType = typeof( XamComboEditor );

        Style style = new Style( typeof( XamComboEditor ) );
        style.Setters.Add( new Setter( XamComboEditor.ValueTypeProperty, typeof( Country ) ) );
        style.Setters.Add( new Setter( XamComboEditor.ItemsProviderProperty, countriesIP ) );
        style.Seal( );

        settings.EditorStyle = style;
    }

    public class DataItem : System.ComponentModel.INotifyPropertyChanged
    {
        private static int ID_COUNTER = 1;
        private int _id;
        private Country _country;

        public DataItem( )
        {
            _id = ID_COUNTER++;
        }

        public int Id
        {
            get
            {
                return _id;
            }
        }

        public Country Country
        {
            get
            {
                return _country;
            }
            set
            {
                if ( _country != value )
                {
                    _country = value;

                    if ( null != this.PropertyChanged )
                        this.PropertyChanged( this, new PropertyChangedEventArgs( "Country" ) );

                    Debug.WriteLine( "New Value = " + _country.Name );
                }
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    }

    public class Country
    {
        private string _shortName, _name;

        public Country( string shortName, string name )
        {
            _shortName = shortName;
            _name = name;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public string ShortName
        {
            get
            {
                return _shortName;
            }
        }

        public override int GetHashCode( )
        {
            return _shortName.GetHashCode( ) ^ _name.GetHashCode( );
        }

        public override bool Equals( object obj )
        {
            Country c = obj as Country;

            return null != c && _shortName == c._shortName && _name == c._name;
        }
    }

     

    Hope this helps,

    Sandip 

Children