-1

I have a Listbox where it's items are objects. In these objects I store two colors.

I want to bind these colors with an other object's property, but how can I achieve this?

The listbox looks like this:

Listbox1.Items.Add(new ColorAndMoreClass(Color.Red, Color.Blue));

Far away, in an other class there is a property which I'd like to bind to.

How can I do that?

2
  • 1
    what is the source, what is the target? is it one-way or two-way binding? what should trigger changes?
    – nikis
    Mar 27, 2015 at 10:09
  • It would be a two-way binding. The other class would store the settings, and the ColorAndMoreClass's properties would be changing according to it. But the user can change the colors, and these changes have to affect the other class. Mar 27, 2015 at 10:25

1 Answer 1

0

Your rootclass could look like this.

In the class you have a object representing a different Class.

  public class ColorAndMoreClass: INotifyPropertyChanged
    {
        private Color _c;
        private Color _c2;
        private OtherClass _example;

        public ColorAndMoreClass(Color c, Color c2)
        {
            _c= c;
            _c2 = c2;
        }

        public OtherClass example
        {
           get { return _example }
           set
           {
               _example = value;
               OnPropertyChanged("example");
           }
        }

        public Color c
        {
            get { return _c; }
            set
            {
                _c= value;
                OnPropertyChanged("c");
            }
        }

        public Color c2
        {
            get { return _c2; }
            set
            {
                _c2 = value;
                OnPropertyChanged("c2");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }
    }

Your other class could look like this. I just took a simple string.

    public class OtherClass : INotifyPropertyChanged
    {
         private String _someOtherProperty;

         public OtherClass(){}

            public String someOtherProperty
            {
                get { return _someOtherProperty; }
                set
                {
                    _someOtherProperty= value;
                    OnPropertyChanged("someOtherProperty");
                }
            }


         public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string info)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(info));
                }
            }
    }

In your Code behind make a property the Listbox can bind to

public List<ColorAndMoreClass>> ListOfColorAndMore{ get; set; }

public Window1() 
{ 
    ListOfColorAndMore = GetDataThatFillsUpTheProperty(); 
    InitializeComponent(); 
    DataContext = this;
} 

Your XAML could then look like this. The Datatemplate is used to tell XAML how to display your object.

    <Grid> 
        <ListBox ItemsSource={Binding ListOfColorAndMore}>
          <DataTemplate x:Key="myTaskTemplate">
             <StackPanel>
               <TextBlock Text="{Binding Path=c.R}" />
               <TextBlock Text="{Binding Path=c2.R}"/>
               <TextBlock Text="{Binding Path=example.someOtherProperty}"/>
             </StackPanel>
          </DataTemplate>
        </ListBox>
    </Grid> 

I hope it is this that you mean. But your question is not that clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.