简体   繁体   中英

WPF : Binding a collection to an itemscontrol

iv'e got a dll consisting of 3 classes which represent a board game :

   Board , Pipe , Checker 

the board is a collection of pipes and the pipes are a collection of checkers

an opponent movement will consist of removing a checker from one pipe and placing it in another .

my board is represented by a grid panel with 23 items-controls , i need to bind each item-control to a pipe so as to display the movements on the UI

the player movement would be done by a drop drag between the itemcontrols (haven't gotten around to that part yet)

the Checker Class :

  public Checker 
  {
      public bool IsMine{get; set;} 
  }

the IsMine property needs to be used to determine the Checkers Color .

the Pipe Class : holds an ObservableCollection of checkers

  public class Pipe : INotifyPropertyChanged
  {
    public Pipe()
    {
        checkers = new ObservableCollection<Checker>();
    }

    private  ObservableCollection<Checker> checkers {get;set;}
    public ObservableCollection<Checker> Checkers
    {
        get { return checkers; }
        set
        {                               
            checkers = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Checkers"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;            
 }

what i need to do is create a 2-way binding between each itemscontrol and it's corresponding Pipe item the add and remove items when the collection changes

i also need a custom converter that will check the IsMine Property and set the objects color accordingly (the itemsControl DataItemTemplate is consist of an Ellipse which would represent each checker).

till now iv'e just populates the collections and set them as the items source for each items control

for example :

   Pipe23.ItemsSource = board.Pipes[23].Checkers;

i am faced with 2 challenges : (1) ether create a 2-way binding with the OwnerToColor converter in code behind which i couldn't find an example of how this is done .

(2) create this binding in markup but then how could i initialize the starting items in the appropriate pipes

for example :

pipe23 starts with 2 checkers 

board.Pipes[23].Checkers.Add(new Checker(true));
board.Pipes[23].Checkers.Add(new Checker(true));   

any good examples for 2-way binding between collection and itemsControl would be most appreciated .

the itemsControl :

    <ItemsControl Grid.Row="0" Grid.Column="0" Name="Pipe23"  ItemTemplate="{StaticResource PipeDataItem}"/>

the DataTemplate :

     <DataTemplate x:Key="PipeDataItem" >
          <Ellipse Width="45" Height="45" Fill="{Binding IsMine,Converter={StaticResource MyOwnerToColorConverter}}"></Ellipse> 
     </DataTemplate>

Instead of setting the ItemsSource directly, you should create a binding.

Binding b = new Binding();
b.Source = board.Pipes[23];
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b.Path = new PropertyPath("Checkers");
Pipe23.SetBinding(ListBox.ItemsSourceProperty, b);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM