views:

25

answers:

2

hey there i am having problem i have

   List<List<memoryCard>> 

that i want to show in my xmal in a button how can I bind my button to the data i want thos is my usercontrol :

<

    <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
    <Grid>
  <!--i think this is the place where i make mistake :-->
            <TextBlock Text="{Binding Path=CardWasfounded}"/>
            <Rectangle Margin="4,5,8,2" Stroke="Black" RadiusX="45" RadiusY="45" StrokeThickness="3"/>
    </Grid>
    </ControlTemplate>

    <DataTemplate x:Key="DataTemplate_Level2">
        <Button Content="{Binding}" Height="40" Width="50" Margin="4,4,4,4"  Template="{DynamicResource ButtonControlTemplate1}"/>
    </DataTemplate>

<DataTemplate x:Key="DataTemplate_Level1">
    <ItemsControl ItemsSource="{Binding }" ItemTemplate="{DynamicResource DataTemplate_Level2}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</DataTemplate>

I want every button to have binding to this memory card

 class memoryCard : INotifyPropertyChanged
{
    #region c'tor
    public memoryCard(Brush _buttonColor)
    {
        buttonColor=_buttonColor;
    }
    #endregion

    #region allReadyFoundedCard

    bool cardWasfounded = false;
        public bool CardWasfounded
        {
            get
            {
                return cardWasfounded;
            }
            set
            {
                if (cardWasfounded != value)
                {
                    cardWasfounded = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this,
                        new PropertyChangedEventArgs("cardWasfounded"));

                    }
                }
            }
        }
        #endregion 

    #region colorofbutton
        string name = "sdasdas";
        public Brush buttonColor;
        public Brush ButtonColor
        {
            get
            {
                return buttonColor;
            }
            set
            {
                if (buttonColor != value)
                {
                    buttonColor = value;
                    if (PropertyChanged != null) PropertyChanged(this,
                        new PropertyChangedEventArgs("buttonColor"));
                }
            }
        }
        #endregion

    #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
}

whom i want to bind to one of my grids this way :

using this mainwindow class:

 public MainWindow()
    {
        List<List<memoryCard>> lsts = new List<List<memoryCard>>();

        for (int i = 0; i < 5; i++)
        {
            lsts.Add(new List<memoryCard>());

            for (int j = 0; j < 5; j++)
            {
                lsts[i].Add(new memoryCard(Brushes.Green));
            }
        }

        InitializeComponent();

        lst.ItemsSource = lsts; 
    }
+1  A: 

Alright, so from what i've gathered you've got a collection of collections of a custom data type which contains a colour that you'd like to bind to.

So heres a small demo that you should (hopefully) be able to expand.

XAML:

<ItemsControl ItemsSource="{Binding Path=MyCollection}" 
              Height="300" Width="600">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander Header="Open Me">
                <ItemsControl DataContext="{Binding}" ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button DataContext="{Binding}"
                                    Background="{Binding Path=ButtonColor}"
                                    Content="{Binding Path=CardWasFounded}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

And in the code behind:

public ObservableCollection<List<memoryCard>> MyCollection {get; set;}

public MainWindow()
{
    DataContext = this;
    MyCollection = new ObservableCollection<List<memoryCard>>();

    for (int i = 0; i < 5; i++)
    {
        List<memoryCard> list = new List<memoryCard>();

        for (int j = 0; j < 5; j++)
        {
            list.Add(new memoryCard(Brushes.Green));
        }
        MyCollection.Add(list);
    }

    InitializeComponent();
}

Is this similar to what you're trying to do?

Val
thank you it seemes like the right path to walk through but it doesnt work for me , can you edit it exactly as it's in your project?
yoav.str
I've updated an issue in the top collection binding in the XAML. i left in my test collection instead of binding to MyCollection. Try this new solution. if there is still a problem, ill break it down into finer chunks.
Val
@Val thank you so much you !!!
yoav.str
A: 

When you raise the PropertyChanged event your parameter name (the string you pass in event arguments) must match exactly to the property you bind to. This involves case sensitivity.

You do new PropertyChangedEventArgs("buttonColor") when the property is actually ButtonColor. This will make the WPF binding system ignore the event (since it determines it doesn't have a matching binding so it doesn't have to do anything). You have the same problem with CardWasFounded

Isak Savo