Mehrdad Kamali
Mehrdad Kamali

Reputation: 87

Binding margin of a textbox in Usercontrol

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

Upvotes: 0

Views: 50

Answers (1)

mmm
mmm

Reputation: 80

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

Upvotes: 1

Related Questions