我想通过在数据网格中使用带有随机边距的usercontrol来添加一堆文本框。
要做到这一点,我尝试在usercontrol中绑定边距并将其添加到主窗口中,但问题是它在一些具有相同边距的情况下更改边距。
我应该如何正确绑定它?
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]);
}
}
}
答案 0 :(得分:1)
只有在我运行您的代码后,我才明白您的问题是什么。 你的问题在于随机函数。
您需要保留一个Random实例并继续在同一个实例上使用Next。所以你要做的就是在for循环之外得到新的随机数。
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]);
}
}
您可以在以下位置详细了解此问题: Random number generator only generating one random number