I want to add a bunch of textbox by using a usercontrol with random margins in a datagrid.
To do that I tried to bind margin in usercontrol and add them in the mainwindow but the problem is it change the margin after a few of them with same margin.
how should I bind this properly?
UserControl XAML:
<UserControl x:Class="WpfApplication23.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication23"
mc:Ignorable="d" >
<Grid>
<TextBox x:Name="textBox" Background="Red" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="300" Margin="{Binding TextBoxMargin, Mode=TwoWay}"/>
</Grid>
UserControl C#:
public partial class UserControl1 : UserControl
{
public Thickness TextBoxMargin { get; set; }
public UserControl1()
{
InitializeComponent();
}
}
}
MainWindow XAML:
<Window x:Class="WpfApplication23.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication23"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="500">
<Grid>
<DataGrid AutoGenerateColumns="False"
x:Name="dataGrid"
IsReadOnly="True"
CanUserAddRows="False"
CanUserDeleteRows="False"
>
<DataGrid.Resources>
<DataGridTemplateColumn x:Key="TemplateColumn" Header="Usercontrol">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:UserControl1/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Resources>
<DataGrid.Columns >
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
MainWindows C#:
public partial class MainWindow : Window
{
UserControl1[] UV = new UserControl1[101];
UserControl1[] AB = new UserControl1[101];
public MainWindow()
{
InitializeComponent();
AA();
}
private void AA()
{
DataGridTemplateColumn templateColumn =
(DataGridTemplateColumn)dataGrid.Resources["TemplateColumn"];
dataGrid.Columns.Add(templateColumn);
for (int i = 0; i <= 100; i++)
{
UV[i] = new UserControl1();
Random len = new Random();
UV[i].TextBoxMargin = new Thickness
(
len.Next(0, 50), len.Next(0, 0), len.Next(0, 50), len.Next(0, 0)
);
UV[i].textBox.Margin = UV[i].TextBoxMargin;
dataGrid.Items.Add(UV[i]);
}
}
}
Only after I run your code I understand what is your problem.
Your problem is with the random function.
You need to keep a single Random instance and keep using Next on the same instance. so all you have to do is get the new random outside of the for loop.
private void AA()
{
DataGridTemplateColumn templateColumn =
(DataGridTemplateColumn)dataGrid.Resources["TemplateColumn"];
dataGrid.Columns.Add(templateColumn);
Random len = new Random();
for (int i = 0; i <= 100; i++)
{
UV[i] = new UserControl1();
UV[i].TextBoxMargin = new Thickness
(
len.Next(0, 50), len.Next(0, 0), len.Next(0, 50), len.Next(0, 0)
);
UV[i].textBox.Margin = UV[i].TextBoxMargin;
dataGrid.Items.Add(UV[i]);
}
}
You can read more about this problem in:
Random number generator only generating one random number
Related
private Queue<Uri> playList = new Queue<Uri>();
playList.Enqueue(new Uri(#"D:\media1"));
playList.Enqueue(new Uri(#"D:\media2"));
playList.Enqueue(new Uri(#"D:\media3"));
Here I am making the playlist hard coded. But I want to populate the playList from a string array. How is it possible?
My string array is string[] mylist=new string[3];
mylist[0]=#"D:\media1;
mylist[1]=#"D:\media2;
mylist[2]=#"D:\media3;
I have done this:
for (int i = 0; i < mylist.Length; i++)
{
playList.Enqueue(new Uri(mylist[i]));
mediaelement.play();
}
But it is only playing the last media.....
The problem is that MediaElement doesn't actually have a playlist feature, but you can easily implement it yourself. Implement the MediaEndedEvent to your mediaElement and play the next element in your string array. My xaml code:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="320*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<MediaElement x:Name="mediaelement" Grid.RowSpan="1" LoadedBehavior="Manual" MediaEnded="mediaelement_MediaEnded"/>
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
<Button x:Name="btnPlay" Content="Next" Click="mediaelement_MediaEnded" Width="50" Height="25" Margin="5"/>
</StackPanel>
</Grid>
</Window>
And the C# code:
public partial class MainWindow : Window
{
private string[] mylist = new string[3];
public MainWindow()
{
InitializeComponent();
mylist[0] = #"D:\media1";
mylist[1] = #"D:\media1";
mylist[2] = #"D:\media1";
mediaelement.Source = new Uri(mylist[0]);
mediaelement.Play();
}
private void mediaelement_MediaEnded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < mylist.Length - 1; i++)
{
Uri current = new Uri(mylist[i]);
if (mediaelement.Source == current)
{
mediaelement.Source = new Uri(mylist[i + 1]);
break;
}
}
mediaelement.Play();
}
}
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="WpfApplication1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="637" Width="1889">
<Grid RenderTransformOrigin="0.505,0.844" Margin="0,0,0,19">
<CheckBox x:Name="cbFilter" Content="CheckBox" HorizontalAlignment="Left" Margin="15,410,0,0" VerticalAlignment="Top" Width="100" Height="25"/>
<ComboBox x:Name="cbFilterMode" HorizontalAlignment="Left" Margin="95,410,0,0" VerticalAlignment="Top" Width="120">
<TextBlock>=</TextBlock>
<TabItem/>
</ComboBox>
<TextBox x:Name="txtFilterValue" HorizontalAlignment="Left" Height="23" Margin="225,409,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<GroupBox x:Name="groupBox" Header="GroupBox" HorizontalAlignment="Left" Margin="225,10,0,0" VerticalAlignment="Top" Width="1110" Height="385">
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="245,105,0,0" VerticalAlignment="Top" Height="230" Width="645"/>
</GroupBox>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="385" Margin="10,10,0,0" VerticalAlignment="Top" Width="230" FontFamily="Segoe UI Black" Grid.Column="10">
<ListBox.ItemTemplate >
<DataTemplate>
<CheckBox x:Name="qqq" Content="{Binding }" IsChecked="{Binding IsChecked}" Click="CheckBox_Click"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Code behind:
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
var cb = sender as CheckBox;
var item = cb.IsChecked;
listBox.SelectedItem = item;
if (listBox.SelectedIndex==0) return;
string query = "SELECT NON EMPTY { [Measures].[Число Подписки] } ON COLUMNS, NON EMPTY {(";
foreach (string i in listBox.Checked)
{
query += " " + i.ToString() + ".ALLMEMBERS *";
}
query = query.Remove(query.Length - 2);
query += " )} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Почта] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS";
UpdateChart(query);
}
It turns out only to withdraw all at once, and how to do that would be deduced by one (ie. If checkBox == true, then we conclude that measurement of the cube)
If you have a such model class:
public class FooClass
{
public bool IsChecked { get; set; }
}
and populating of ListBox looks like that:
public MainWindow()//***ctor of Window
{
InitializeComponent();
PopulateListView();
}
private void PopulateListView()
{
IList<FooClass> someValues = new List<FooClass>();
for (int i = 0; i < 10; i++)
{
someValues.Add(new FooClass() { IsChecked=true});
}
listBox.ItemsSource = someValues;
}
Then to get all CheckBox'es values from ListBox you can do:
private void qqq_Click(object sender, RoutedEventArgs e)
{
var e1=listBox.Items;
foreach (var c in listBox.ItemsSource)
{
MessageBox.Show(((FooClass)c).IsChecked.ToString());
}
}
I have image which I set behind code and now I have to set 3 texts under the picture also behind code, maybe somenoe knoe how to make it?
my xaml:
...<Style x:Key="imageStyle" TargetType="{x:Type Image}">
<Setter Property="Height" Value="152px"/>
<Setter Property="Width" Value="762"/>
</Style>
</windows.Resources>
<Grid>
<StackPanel x:Name="StackStyle" Background="#FFFFFF" Margin="30,98,250,150">...
My xaml.cs is:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
Style imgStyle = (Style)Resources["imageStyle"];
var imag = new Image();
imag.Style = imgStyle;
string path = System.AppDomain.CurrentDomain.BaseDirectory + "../../ico.choose-coupon.png";
imag.Source = new BitmapImage(new Uri(path));
StackStyle.Children.Add(imag);
}
}
do not forget that this is one image, and these text have to be under Gray field.
UPDATE:
I get what I want, thats the code:
xaml:
<Viewbox Stretch="Fill">
<Grid>
<StackPanel Orientation="Vertical" x:Name="myStackPanel" Background="#FFFFFF" Height="560" Width="968" HorizontalAlignment="Center" >
<Label Style="{StaticResource Label}" ></Label>
</StackPanel>
</Grid>
</Viewbox>
xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
string path = System.AppDomain.CurrentDomain.BaseDirectory + "../../ico.choose-coupon.png";
Style imgStyle = (Style)Resources["imageStyle"];
var imag = new Image();
imag.Style = imgStyle;
imag.Source = new BitmapImage(new Uri(path));
myStackPanel.Children.Add(imag);
Style textStyle = (Style) Resources["textStyle"];
var text1 = new TextBlock();
var text2 = new TextBlock();
var text3 = new TextBlock();
text1.Style = textStyle;
text2.Style = textStyle;
text3.Style = textStyle;
text1.Text = "% coupon";
text2.Text = "Tara receipt";
text3.Text = "Value coupon";
text1.Margin = new Thickness(135,22,0,0);
text2.Margin = new Thickness(210, 22, 0, 0);
text3.Margin = new Thickness(188, 22, 0, 0);
var TextPanel = new StackPanel();
TextPanel.Orientation = Orientation.Horizontal;
TextPanel.Children.Add(text1);
TextPanel.Children.Add(text2);
TextPanel.Children.Add(text3);
myStackPanel.Children.Add(TextPanel);
}
}
You can use a button style. For the Text and the Image you can set an binding to the propertys. Maybe you write an Style for the button so that looks like in your example.
<Button>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Source}">
<Label Padding="0" Text="{Binding YourText}"></Label>
</StackPanel>
</Button>
Here is an exmaple of simple UserControl:
Add new userControl template to your project.
I am attaching an example of userControl with TextBox and TextBlock:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="WpfApplication1.Example"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480" Width="40" Height="40">
<Grid x:Name="LayoutRoot" Width="40" Height="40" Background="White">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Test " VerticalAlignment="Top" Height="14.32"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="0,17,0,0" TextWrapping="Wrap" Text="Text" VerticalAlignment="Top" Width="40"/>
</Grid>
</UserControl>
Then in your window, just call to your userControl as much as you want(You can pass objects to it too):
<local:Example HorizontalAlignment="Left" Margin="16.4,22.4,0,0" VerticalAlignment="Top"/>
<local:Example HorizontalAlignment="Left" Margin="16.4,22.4,0,0" VerticalAlignment="Top"/>
<local:Example HorizontalAlignment="Left" Margin="16.4,22.4,0,0" VerticalAlignment="Top"/>
I want to build a monthview like the native wp8 calendar has.
But I am stuck with the incrementally loading of more Pivotitems when reaching the the end of the initial loaded Pivotitems.
I am kind of confused, how this could be achieved.
Here is my xaml so far:
<phone:Pivot x:Name="MonthViewPivot" ItemsSource="{Binding Months}" Margin="0" Loaded="Pivot_Loaded" SelectionChanged="Pivot_SelectionChanged">
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Padding="0,0,0,0" Text="{Binding Name}" RenderTransformOrigin="0.5,0.5">
<TextBlock.RenderTransform>
<CompositeTransform TranslateX="0" TranslateY="24"/>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<Grid x:Name="MonthViewGrid" Height="480" Loaded="MonthViewGrid_Loaded" VerticalAlignment="Top" Margin="-10,70">
<StackPanel Orientation="Horizontal" Height="30" VerticalAlignment="Top" Margin="0,-30" >
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Mo</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Di</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Mi</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Do</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Fr</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Sa</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">So</TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
So here is my solution so far.
I still need to increase the count of PivotItems, but 12 seems to high for a fast UX. I think 5 will do.
UserControl:
<UserControl x:Class="Pocal.MonthViewUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot">
<Grid x:Name="MonthViewGrid" Margin="0,60,0,0">
<StackPanel Orientation="Horizontal" Height="30" VerticalAlignment="Top" Margin="0,-30" >
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Mon</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Tue</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Wed</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Thu</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Fri</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Sat</TextBlock>
<TextBlock Width="68.5" Padding="6,0" Foreground="Gray">Sun</TextBlock>
</StackPanel>
</Grid>
</Grid>
UserControl Code Behind:
public partial class MonthViewUserControl : UserControl
{
public MonthViewUserControl()
{
InitializeComponent();
}
public void loadGridSetup(DateTime dt)
{
TextBlock txt = new TextBlock();
txt.Text = dt.ToShortDateString();
txt.Tap += dayTap;
(MonthViewGrid as Grid).Children.Add(txt);
}
void dayTap(object sender, System.Windows.Input.GestureEventArgs e)
{
DateTime dt = (DateTime)((FrameworkElement)sender).DataContext;
MonthView.CurrentPage.navigateBackToDay(dt);
}
}
MonthView XAML:
<phone:PhoneApplicationPage
x:Class="Pocal.MonthView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:local="clr-namespace:Pocal"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="False">
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Style="{StaticResource PhoneTextTitle3Style}" VerticalAlignment="Top" Margin="24,24" FontWeight="SemiBold">2014</TextBlock>
<phone:Pivot x:Name="MonthsPivot" Margin="0,36,0,0" SelectionChanged="Pivot_SelectionChanged">
</phone:Pivot>
</Grid>
MonthView COde Behind:
public partial class MonthView : PhoneApplicationPage
{
public static MonthView CurrentPage;
PivotItem pivotItem;
MonthViewUserControl monthViewUserControl;
public MonthView()
{
InitializeComponent();
CurrentPage = this;
addFirstThreePivots();
}
private void addFirstThreePivots()
{
DateTime dt = DateTime.Now.Date;
DateTime dt2 = dt.AddMonths(1);
DateTime dt3 = dt.AddMonths(-1);
addPivotItem(dt);
addPivotItem(dt2);
addPivotItem(dt3);
}
private void addPivotItem(DateTime dt)
{
monthViewUserControl = new MonthViewUserControl();
monthViewUserControl.loadGridSetup(dt);
pivotItem = new PivotItem();
pivotItem.Content = monthViewUserControl;
pivotItem.DataContext = dt;
pivotItem.Margin = new Thickness(0, 0, 0, 0);
pivotItem.Header = dt.ToString("MMMM");
MonthsPivot.Items.Add(pivotItem);
}
private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DependencyObject selectedPivotItem = ((Pivot)sender).ItemContainerGenerator.ContainerFromIndex(((Pivot)sender).SelectedIndex);
if (selectedPivotItem == null)
return;
DateTime removedDateTime = (DateTime)(e.RemovedItems[0] as PivotItem).DataContext;
DateTime addedDateTime = (DateTime)(e.AddedItems[0] as PivotItem).DataContext;
if (removedDateTime < addedDateTime)
{
forwardPan(addedDateTime);
}
else
backwardPan(addedDateTime);
}
private void forwardPan(DateTime addedDateTime)
{
PivotItem nextPivotItem = (PivotItem)getNextPivotItem();
DateTime newDateTime = addedDateTime.AddMonths(1);
MonthViewUserControl monthViewItem = new MonthViewUserControl();
monthViewItem.loadGridSetup(newDateTime);
nextPivotItem.DataContext = newDateTime;
nextPivotItem.Content = monthViewItem;
nextPivotItem.Header = newDateTime.ToString("MMMM");
}
private DependencyObject getNextPivotItem()
{
int index = ((Pivot)MonthsPivot).SelectedIndex;
int nextIndex;
if (index == 2)
{
nextIndex = 0;
}
else
nextIndex = index + 1;
DependencyObject nextPivotItem = ((Pivot)MonthsPivot).ItemContainerGenerator.ContainerFromIndex(nextIndex);
return nextPivotItem;
}
private void backwardPan(DateTime addedDateTime)
{
PivotItem previousPivotItem = (PivotItem)getPreviousPivotItem();
DateTime newDateTime = addedDateTime.AddMonths(-1);
MonthViewUserControl monthViewItem = new MonthViewUserControl();
monthViewItem.loadGridSetup(newDateTime);
previousPivotItem.DataContext = newDateTime;
previousPivotItem.Content = monthViewItem;
previousPivotItem.Header = newDateTime.ToString("MMMM");
}
private DependencyObject getPreviousPivotItem()
{
int index = ((Pivot)MonthsPivot).SelectedIndex;
int previousIndex;
if (index == 0)
{
previousIndex = 2;
}
else
previousIndex = index - 1;
DependencyObject previousPivotItem = ((Pivot)MonthsPivot).ItemContainerGenerator.ContainerFromIndex(previousIndex);
return previousPivotItem;
}
public void navigateBackToDay(DateTime dt)
{
App.ViewModel.GoToDate(dt);
NavigationService.Navigate(new Uri("/Mainpage.xaml?GoToDate=", UriKind.Relative), dt);
}
}
I am trying to create Rectangle programmaticallyin silverlight as fallows.
C#
private Boolean Working = false;
const int scale = 4;
const int size = 50;
Int32[] data = new Int32[size];
Rectangle[] lines = new Rectangle[size];
private void button1_Click(object sender, RoutedEventArgs e)
{
canvas1.Children.Clear();
for (int i = 0; i < data.Length; i++)
{
data[i] = i;
lines[i] = new Rectangle()
{
Height=i*scale,
Width = 10,
StrokeThickness=5,
Stroke = new SolidColorBrush(Colors.Red),
Name=i.ToString(),
};
canvas1.Children.Add(lines[i]);
}
}
Now the problem is that all the rectangle are created with the same height and width?.
XAML
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500">
<Canvas x:Name="canvas2" Background="White">
<Canvas x:Name="canvas1" Background="White"></Canvas>
<Button Content=" Generate" Height="38" Name="button1" Width="75" Click="button1_Click" Margin="0,352,245,24" HorizontalAlignment="Right" Canvas.Left="12" Canvas.Top="85" />
<Button Content="Shuffle" Height="38" HorizontalAlignment="Left" Name="button2" Margin="12,352,0,24" Click="button2_Click_1" Canvas.Left="81" Canvas.Top="85" Width="71" />
<Button Canvas.Left="181" Canvas.Top="437" Content="Bubble Sort" Height="38" Name="button3" Width="109" Click="button3_Click" />
</Canvas>
</UserControl>
Screenshot
Actually, everything works fine. All rectangles have different height. But you forget to move them, so they start overlapping. Change a little code and you'll see:
lines[i] = new Rectangle()
{
Height = i * scale,
Width = 10,
StrokeThickness = 5,
Stroke = new SolidColorBrush(Colors.Red),
Name = i.ToString(),
};
lines[i].Margin = new Thickness(11 * i, 0, 0, 0);
Result