for (int i = 0; i < 6; i++) { Brique b = new Brique { Id = i + 1 };//En une ligne tu instancies et affecte la propriété Id VisualBrique.Add(b); }
<ItemsControl ItemsSource="{Binding VisualBrique}" Height="65" Width="45"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Vertical"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border BorderThickness="2" Height="20" Width="20" BorderBrush="Red" Background="LightGray"> <TextBlock Text="{Binding Id}" Foreground="White"/> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
public void DrawWall() { VisualBrique.Clear(); for (int i = 0; i < 6; i++) ////6 pour simplifier l'exemple en fait = (Height=2 X Width=3 propriété du mur) { Brique b = new Brique(); b.Id = i + 1; VisualBrique.Add(b); } }
VisualBrique.Clear(); for (int i = 0; i < Mur.Height * Mur.Width; i++) { Brique b = new Brique { Id = i + 1 }; VisualBrique.Add(b); } // code à rajouter int Idbrique; int NombreImpair = Mur.Width - 1; int NombrePair = 0; for (int i = 0; i < VisualBrique.Count -1; i += 2) { Idbrique = VisualBrique[i].Id; Idbrique -= NombrePair; VisualBrique[i].Id = Idbrique; Idbrique = VisualBrique[i + 1].Id; Idbrique += NombreImpair; VisualBrique[i + 1].Id = Idbrique; NombreImpair--; NombrePair++; }
using System.Collections.Generic; using System.Windows.Forms; namespace Test { public partial class Form1 : Form { private List<Brique> VisualBrique = new List<Brique>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, System.EventArgs e) { Mur M = new Mur { Height = 2, Width = 5 }; // un mur en largeur et hauteur ( on peut modifier la largeur uniquement ) VisualBrique.Clear(); for (int i = 0; i < M.Height * M.Width; i++) { Brique b = new Brique { Id = i + 1 }; VisualBrique.Add(b); } int Idbrique; int NombreImpair = M.Width - 1; int NombrePair = 0; for (int i = 0; i < VisualBrique.Count -1; i += 2) { Idbrique = VisualBrique[i].Id; Idbrique -= NombrePair; VisualBrique[i].Id = Idbrique; Idbrique = VisualBrique[i + 1].Id; Idbrique += NombreImpair; VisualBrique[i + 1].Id = Idbrique; NombreImpair--; NombrePair++; } } public class Brique { public int Id { get; set; } } public class Mur { public int Width { get; set; } public int Height { get; set; } } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question<ListBox ItemsSource="{Binding VisualBrique }" SelectionMode="Multiple" Padding="-1" Margin="0" BorderThickness="0"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="{Binding Height}" Columns="{Binding Width}" Margin="0" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Opacity" Value="0.1"/> <Setter Property="Background" Value="Transparent"/> </Trigger> </Style.Triggers> <Setter Property="Padding" Value="0"/> <Setter Property="Margin" Value="0"/> <Setter Property="BorderThickness" Value="10"/> <Setter Property="Background" Value="DarkGray"/> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="Cursor" Value="Hand"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="IsSelected" Value="{Binding IsSelected}"/> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate DataType="{x:Type vm:ViewBriqueViewModel}"> <TextBox Foreground="White" Text="{Binding Id}" FontSize="200" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" IsReadOnly="True" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="Transparent" > </TextBox> </DataTemplate> </ListBox.ItemTemplate> </ListBox>