2017-06-25 3 views
0

データグリッド内のランダムな余白を持つ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> 

ユーザーコントロールC#:

public partial class UserControl1 : UserControl 
{ 
    public Thickness TextBoxMargin { get; set; } 

    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

} 

}

メインウィンドウXA ML:

<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

'モード= TwoWay'のどのような目的で、あなたの' Margin'結合に? – Maxim

+0

@maximテストのために追加したものは何もありません –

答えて

1

私はあなたのコードを実行した後にのみ、私はあなたの問題が何であるかを理解しています。 問題はランダム関数です。

1つのランダムインスタンスを保持し、同じインスタンスで[次へ]を使用する必要があります。だからあなたがしなければならないのは、新しいループを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

関連する問題