Click here to Skip to main content
15,902,299 members
Home / Discussions / C#
   

C#

 
AnswerRe: is a c# totally based on OOPS concept? Pin
Richard MacCutchan8-Sep-17 19:23
mveRichard MacCutchan8-Sep-17 19:23 
AnswerRe: is a c# totally based on OOPS concept? Pin
Dave Kreskowiak9-Sep-17 7:32
mveDave Kreskowiak9-Sep-17 7:32 
JokeRe: is a c# totally based on OOPS concept? Pin
Mycroft Holmes9-Sep-17 14:19
professionalMycroft Holmes9-Sep-17 14:19 
GeneralRe: is a c# totally based on OOPS concept? Pin
Dave Kreskowiak9-Sep-17 14:22
mveDave Kreskowiak9-Sep-17 14:22 
QuestionHow Can I Add Membership To My Program Pin
istemihan8-Sep-17 11:59
istemihan8-Sep-17 11:59 
AnswerRe: How Can I Add Membership To My Program Pin
Richard MacCutchan8-Sep-17 19:21
mveRichard MacCutchan8-Sep-17 19:21 
Answerjoin and sum in linqu Pin
Member 109061148-Sep-17 5:28
Member 109061148-Sep-17 5:28 
QuestionC# and XAML bindings - Need Help with events and dependent Drop Down Lists Pin
Member 133487177-Sep-17 4:45
Member 133487177-Sep-17 4:45 
I am having a STUPID amount of trouble with this. Just when I think I have it figured out - I don't. One thing I have found is that events seem to find a seemingly random order and nested events just refuse the way you expect them to and do not trigger in the order you might expect.

Basically : I have XAML form. On this form I have (among other controls), several bound drop down list controls. The first of these is not dependent on any other, the contents of the second drop down list is dependent on the selected value of the first, the third is dependent on the selection in the second and so on.

Problem 1 : The list contents vary in size depending on other form values. This means that when I am creating a list it sometimes throws an index out of bounds error if the previous index was 4 and the new list only contains 2 entries

Problem 2 : I keep getting stuck in loops. Updating the drop list boxes also forces a property change in the selected value which then triggers an update to my list and contents and the cycle continues

Problem 3 : trying to throw OnPropertyChanged at specific points in my code seems to just completely confuse the process with the order that the events are thrown and trapped making very little sense.

Problem 4 : the list goes on.

SO! I arrive here at Code Project. I have make up some sample code and keep it down to an minimum as everything I have done to date seems to be a complete mess. I have also included lines of XAML to show the way the drop down lists are bound to the data.

Part of the issue that is catching me out is the need to also validate and set accordingly the selected property and list contents of the combo boxes. i.e. if the Category is changed I need to update the contents of the Colours combo box and ALSO the selected combo box item. This in turn requires the Shapes combo box and relevant combo box to be validate accordingly.

I am hoping that the below is sufficient to illustrate what is seemingly a simple task. I didn't want to bury this question with a mountain of rubbish code that I have ended up with trying a bunch of experiments to get this to work. I've been working on this ALL DAY today and a fair portion of yesterday so my eyes are getting blurry and productivity has all but left me.

XAML bindings :

XML
<ComboBox Margin="2,2,2,2" BorderThickness="0,0,0,1" Grid.Row="3" Grid.Column="1" ItemsSource="{Binding DataCategoryList}" SelectedIndex="{Binding DataCategory}" DisplayMemberPath="Name" />
<ComboBox Margin="2,2,2,2" BorderThickness="0,0,0,1" Grid.Row="4" Grid.Column="1" ItemsSource="{Binding DataColourList}" SelectedIndex="{Binding DataColour}" DisplayMemberPath="Name"/>
<ComboBox Margin="2,2,2,2" BorderThickness="0,0,0,1" Grid.Row="5" Grid.Column="1" ItemsSource="{Binding DataShapeList}" SelectedIndex="{Binding DataShape}" DisplayMemberPath="Name"/>


C#
C#
public class Base : INotifyPropertyChanged
{
  private string _ID;
  private string _Name;

  public string ID { get { return _ID; } set { _ID = value; OnPropertyChanged( "ID" ); } }
  public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged( "Name" ); } }

  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged( string propertyName )
  {
    if( PropertyChanged != null )
      PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
  }
}

public class MyData : INotifyPropertyChanged
{
  private Base _Category;
  private Base _Colour;
  private Base _Shape;

  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged( string propertyName )
  {
    if( PropertyChanged != null )
      PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
  }

  public Base Category
  {
    get { return _Category; }
    set
    {
      _Category = value;
      OnPropertyChanged( "Category" );
    }
  }

  public Base Colour
  {
    get { return _Colour; }
    set
    {
      _Colour = value;
      OnPropertyChanged( "Colour" );
    }
   }
  public Base Shape
  {
    get { return _Shape; }
    set
    {
      _Shape = value;
      OnPropertyChanged( "Shape" );
    }
  }
}

public class MyWindow : INotifyPropertyChanged
{
  private MyData _Data;

  public MyData Data { get { return _Data; } set { _Data = value; } }

  public MyWindow( ) : base( )
  {
    Data = new MyData( )
    {
      Category = new Base( ) { ID = "A", Name = "Category A" },
      Colour = new Base( ) { ID = "RD", Name = "Red" },
      Shape = new Base( ) { ID = "RND", Name = "Round" },
    };

    Data.PropertyChanged += Data_PropertyChanged;
  }

  private void ValidateCategoryChange( string id)
  {
    Base result = null;

    foreach( Base colour in DataColourList )
      if( colour.ID == id )
        result = colour;

    Data.Colour = result ?? DataColourList[ 0 ];
  }

  private void ValidateColourChange( string id )
  {
    Base result = null;

    foreach( Base shape in DataShapeList )
      if( shape.ID == id )
        result = shape;

    Data.Shape = result ?? DataShapeList[ 0 ];
  }

  private void Data_PropertyChanged( object sender, PropertyChangedEventArgs e )
  {
    // Amend and re-throw this to update the UI control.
    OnPropertyChanged( String.Format( "Cable{0}", e.PropertyName ) );
  }

  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged( string propertyName )
  {
    if( PropertyChanged != null )
      PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
  }

  public List<Base> BuildCategoryList( )
  {
    List<Base> result = new List<Base>();

    result.Add( new Base( ) { ID = "A", Name = "Category A" } );
    result.Add( new Base( ) { ID = "A", Name = "Category B" } );

    return result;
  }

  public List<Base> BuildColourList( string categoryID )
  {
    List<Base> result = new List<Base>();

    if( categoryID == "A" )
    {
      result.Add( new Base( ) { ID = "RD", Name = "Red" } );
      result.Add( new Base( ) { ID = "BL", Name = "Blue" } );
    }
    else
    {
      result.Add( new Base( ) { ID = "BK", Name = "Black" } );
      result.Add( new Base( ) { ID = "OR", Name = "Green" } );
      result.Add( new Base( ) { ID = "RD", Name = "Red" } );
      result.Add( new Base( ) { ID = "BL", Name = "Blue" } );
    }

    return result;
  }

  public List<Base> BuildShapeList( string colourID )
  {
    List<Base> result = new List<Base>();

    if( colourID == "RD" || colourID == "BK" )
    {
      result.Add( new Base( ) { ID = "RND", Name = "Round" } );
      result.Add( new Base( ) { ID = "SQR", Name = "Square" } );
      result.Add( new Base( ) { ID = "TNG", Name = "Triangle" } );
    }
    else
    {
      result.Add( new Base( ) { ID = "RCT", Name = "Rectangle" } );
    }

    return result;
  }

  public List<Base> DataCategoryList { get; set; }

  public int DataCategory
  {
    get
    {
      for( int index = 0; index < DataCategoryList.Count; index++ )
        if( Data.Category == DataCategoryList[ index ] )
          return index;

      return -1;
    }
    set
    {
      if( value == -1 ) // Prevent the following from failing with an invalid index;
        return;

      Data.Category.ID = DataCategoryList[ value ].ID;
      OnPropertyChanged( "DataCategory" );
      DataColourList = BuildColourList( Data.Category.ID ); // We need to update the list of colours now that the category has changed
      ValidateCategoryChange( Data.Category.ID );
    }
  }

  public List<Base> DataColourList { get; set; }

  public int DataColour
  {
    get
    {
      for( int index = 0; index < DataColourList.Count; index++ )
        if( Data.Colour == DataColourList[ index ] )
          return index;

      return -1;
    }
    set
    {
      if( value == -1 ) // Prevent the following from failing with an invalid index;
        return;

      Data.Colour.ID = DataColourList[ value ].ID;
      OnPropertyChanged( "DataColour" );
      DataShapeList = BuildShapeList( Data.Colour.ID ); // We need to update the list of colours now that the shape has changed
      ValidateColourChange( Data.Colour.ID );
    }
  }

  public List<Base> DataShapeList { get; set; }

  public int DataShape
  {
    get
    {
      for( int index = 0; index < DataShapeList.Count; index++ )
        if( Data.Category.ID == DataShapeList[ index ].ID )
          return index;

      return -1;
    }
    set
    {
      if( value == -1 ) // Prevent the following from failing with an invalid index;
        return;

      Data.Category = DataShapeList[ value ];
      OnPropertyChanged( "DataShape" );
    }
  }
}

AnswerRe: C# and XAML bindings - Need Help with events and dependent Drop Down Lists Pin
Mycroft Holmes7-Sep-17 13:20
professionalMycroft Holmes7-Sep-17 13:20 
Questionget more than one string in the checkedlistbox using an array C # Pin
Member 133640086-Sep-17 6:56
Member 133640086-Sep-17 6:56 
SuggestionRe: pegar mais de uma string no checkedlistbox usando um arrayList C# Pin
Richard Deeming6-Sep-17 7:07
mveRichard Deeming6-Sep-17 7:07 
QuestionError on xml.Response Pin
Bootzilla335-Sep-17 21:10
Bootzilla335-Sep-17 21:10 
AnswerRe: Error on xml.Response Pin
OriginalGriff5-Sep-17 21:19
mveOriginalGriff5-Sep-17 21:19 
AnswerRe: Error on xml.Response Pin
Pete O'Hanlon6-Sep-17 0:30
mvePete O'Hanlon6-Sep-17 0:30 
SuggestionRe: Error on xml.Response Pin
Richard Deeming6-Sep-17 6:00
mveRichard Deeming6-Sep-17 6:00 
Questionhow to enable the USB COM Port..? Pin
Member 133808373-Sep-17 21:35
Member 133808373-Sep-17 21:35 
QuestionRe: how to enable the USB COM Port..? Pin
Jochen Arndt3-Sep-17 21:44
professionalJochen Arndt3-Sep-17 21:44 
AnswerRe: how to enable the USB COM Port..? Pin
OriginalGriff3-Sep-17 23:18
mveOriginalGriff3-Sep-17 23:18 
GeneralRe: how to enable the USB COM Port..? Pin
Jochen Arndt3-Sep-17 23:39
professionalJochen Arndt3-Sep-17 23:39 
AnswerRe: how to enable the USB COM Port..? Pin
Jochen Arndt3-Sep-17 23:49
professionalJochen Arndt3-Sep-17 23:49 
Questionbinding datagridview with entity framework Pin
Member 132649363-Sep-17 7:17
Member 132649363-Sep-17 7:17 
AnswerRe: binding datagridview with entity framework Pin
Mycroft Holmes3-Sep-17 22:49
professionalMycroft Holmes3-Sep-17 22:49 
QuestionUpdate listbox items when button is clicked Pin
Mario Lukačić3-Sep-17 2:19
Mario Lukačić3-Sep-17 2:19 
AnswerRe: Update listbox items when button is clicked Pin
Richard MacCutchan3-Sep-17 21:14
mveRichard MacCutchan3-Sep-17 21:14 
GeneralRe: Update listbox items when button is clicked Pin
Mario Lukačić4-Sep-17 2:31
Mario Lukačić4-Sep-17 2:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.