2011-06-24 18 views
0

私はMVVMを実践するためにWPFアプリケーションを開発中です。私は、4つのグリッドを持つ必要があります。すべては、 "表示"コントロール(フィールド名を持つTextBlock/RadioButton)と "値"コントロール(フィールドの値を表すために必要なコントロール)。WPFバインドGrid上のSharedSizeGroupが機能しない

別のユーザーコントロールの各グリッドでは、最初の列をすべて同期させる必要があるため、「値」コントロールが画面上に広がり、「表示」コントロールの共有サイズは自動的な幅になりますそれは変わらない。

SharedSizeColumnを定数名に設定すると、すべてのグリッドが完璧で見栄えの良い同期になりますが、これらのグリッドを含むユーザーコントロールがタブ付きビュー間で共有されるため、ビューモデルとのバインドによってSharedSizeColumnを設定する必要があります。再利用のためのモデル、タブ/ビューモデル間でのグリッドの同期化は望ましくありません。 バインディングでSharedSizeGroupを設定すると、SharedSizeGroupがまったく設定されていないように、すべてのグリッドの2つの列が動作します。バインディングをBindingOperationsを使用してバインドを設定しようとしましたが、まだ運がありません。

SharedSizeGroupをバインドする方法、または同じユーザーコントロールを再利用するタブ間でSharedSizeGroupを共有できないようにする方法はありますか?

答えて

0

データバインディングを使用してSharedSizeGroupを使用した完全な実例です。

マークアップ:

<Grid> 
    <StackPanel Grid.IsSharedSizeScope="True" Margin="20"> 
     <Grid HorizontalAlignment="Left"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition SharedSizeGroup="{Binding ColumnA}"/> 
       <ColumnDefinition SharedSizeGroup="{Binding ColumnB}"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock Text="aa" Grid.Column="0" Foreground="Red"/> 
      <TextBlock Text="bbbbbbbb" Grid.Column="1" Foreground="Blue"/> 
     </Grid> 
     <Grid HorizontalAlignment="Left"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition SharedSizeGroup="{Binding ColumnC}"/> 
       <ColumnDefinition SharedSizeGroup="{Binding ColumnD}"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock Text="cccccccc" Grid.Column="0" Foreground="Red"/> 
      <TextBlock Text="dd" Grid.Column="1" Foreground="Blue"/> 
     </Grid> 
    </StackPanel> 
</Grid> 

とコードビハインド:

void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    DataContext = new SharedSizeGroupViewModel 
    { 
     ColumnA = "group1", 
     ColumnB = "group2", 
     ColumnC = "group1", 
     ColumnD = "group2", 
    }; 
} 

とプリミティブビューモデル:

public class SharedSizeGroupViewModel 
{ 
    public string ColumnA { get; set; } 
    public string ColumnB { get; set; } 
    public string ColumnC { get; set; } 
    public string ColumnD { get; set; } 
} 

と、これは、それは次のようになります。

SharedSizeGroup Demo

赤と青の列が並んでいます。

関連する問題