代码之家  ›  专栏  ›  技术社区  ›  yoav.str

wpf将controlitem绑定到集合

  •  0
  • yoav.str  · 技术社区  · 16 年前

       List<List<memoryCard>> 
    

    我想在xmal中的一个按钮中显示如何将我的按钮绑定到我想要的数据这是我的用户控件:

    <

        <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>
    

    我要把每个按钮都绑在这张存储卡上

     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
    }
    

    使用此主窗口类:

     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; 
        }
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   Val    16 年前

    好吧,从我收集的数据来看,你得到了一个自定义数据类型的集合,其中包含了一个你想要绑定到的颜色。

    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>
    

    在暗号里:

    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();
    }
    

    这和你想做的相似吗?

        2
  •  0
  •   Isak Savo    16 年前

    引发PropertyChanged事件时,参数名(传入事件参数的字符串)必须匹配 确切地

    你知道的 new PropertyChangedEventArgs("buttonColor") 当财产实际上 ButtonColor . 这将使WPF绑定系统忽略该事件(因为它确定它没有匹配的绑定,所以不必执行任何操作)。你也有同样的问题 CardWasFounded

    推荐文章