There is a model and a view model:

class model : INotifyPropertyChanged { private int size; public int Size { get { return size; } set { size = value; OnPropertyChanged("Size"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } class viewmodel : INotifyPropertyChanged { public ObservableCollection<model> Size { get; set; } public viewmodel() { Size = new ObservableCollection<model> { new model { Size=9 } }; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } 

MainWindow.xaml code:

 <Window x:Class="mvvm4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox> <ListBox.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="{Binding Path=Size}" Columns="{Binding Path=Size}" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> </Grid> </Window> 

How do I fill the Uniform grid completely with button elements or others? (Ie, fill the grid with buttons of size size to size?

    1 answer 1

     public class ButtonViewModel : INotifyPropertyChanged { private string _name; public string Name //надо же хоть что-то сбиндить =) { get { return _name; } set { _name= value; OnPropertyChanged("Name"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } public class MainViewmodel : INotifyPropertyChanged { private int _size; public int Size //Это должно быть на этом уровне, ведь привязка ListBox идет именно к этой вьюмодели, а уже элементы привязываются к элементам коллекции. { get { return _size; } set { _size= value; OnPropertyChanged("Size"); } } public ObservableCollection<ButtonViewModel> ButtonList{ get; set; } public MainViewmodel() { ButtonList= new ObservableCollection<ButtonViewModel> { // Нужно побольше, чтоб проверить, надо 81 элемент добавить. так что лучше циклом. new ButtonViewModel { Name="Button1" }, new ButtonViewModel { Name="Button2" }, new ButtonViewModel { Name="Button3" }, new ButtonViewModel { Name="Button4" }, new ButtonViewModel { Name="Button5" }, new ButtonViewModel { Name="Button6" }, new ButtonViewModel { Name="Button7" }, new ButtonViewModel { Name="Button8" }, new ButtonViewModel { Name="Button9" }, }; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } 

    Xaml is like that.

      <ListBox ItemsSource="{Binding ButtonList}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="{Binding Path=Size}" Columns="{Binding Path=Size}" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Button Content="{Binding Name}"></Button> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 
    • thanks, and how to fix? - Draktharon
    • I correctly understood that you want to fill the grid 9 by 9? - Sergey
    • Yes, they understood correctly - Draktharon
    • Well, now I will change the answer. - Sergey
    • Everything, the code checked - the worker. - Sergey