2012-08-28 4 views
1

나는 중요한 것을 놓치고 있다고 생각하지만, 나는 그걸 알아낼 수 없습니다. 여러 개의 그리드가 같은 열 너비를 공유하도록하고 싶습니다. 따라서 sharedsizegroups를 사용하고 있습니다. 그러나 나는 그걸 작동시키지 못합니다.그리드 SharedSizeScope가 작동하지 않습니다.

XAML :

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication1" 
     Title="MainWindow" 
     Width="525" 
     Height="350"> 
    <StackPanel x:Name="sg" 
       local:exGrid.IsSharedSizeScope="True" 
       Orientation="Vertical"> 
     <Button Click="Button_Click" Content="Click Me" /> 
    </StackPanel> 
</Window> 

코드 숨김

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      sg.Children.Add(new exGrid("short", "loooooooooooooooooooooooooong", "a")); 
      sg.Children.Add(new exGrid("veeeeeeeeeeeeeeeeeeery loooooooooooooooooooooooooong", "short", "a")); 
     } 
    } 
} 

exGrid.cs

namespace WpfApplication1 
{ 
    class exGrid : Grid 
    { 
     public exGrid(string a, string b, string g) 
     { 
      this.SetValue(exGrid.IsSharedSizeScopeProperty, true); 

      for (int i = 1; i <= 2; i++) 
      { 
       this.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, SharedSizeGroup = g }); 
      } 

      this.SetValue(exGrid.ShowGridLinesProperty, true); 

      TextBlock tx1 = new TextBlock(); 
      tx1.Text = a; 

      TextBlock tx2 = new TextBlock(); 
      tx2.Text = b; 

      tx1.SetValue(exGrid.ColumnProperty, 0); 
      tx2.SetValue(exGrid.ColumnProperty, 1); 

      this.Children.Add(tx1); 
      this.Children.Add(tx2); 
     } 
    } 
} 

답변

2

참조 MSDN 예 : 당신은 단지에 IsSharedSizeScopeProperty를 설정해야 http://msdn.microsoft.com/fr-fr/library/system.windows.controls.grid.issharedsizescope.aspx

일 각 그리드가 아닌 그리드 컨테이너 :

namespace WpfApplication1 
{ 
    class exGrid : Grid 
    { 
     public exGrid(string a, string b, string g) 
     { 
      // this.SetValue(exGrid.IsSharedSizeScopeProperty, true); 

      for (int i = 1; i <= 2; i++) 
      { 
       this.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, SharedSizeGroup = g }); 
      } 

      this.SetValue(exGrid.ShowGridLinesProperty, true); 

      TextBlock tx1 = new TextBlock(); 
      tx1.Text = a; 

      TextBlock tx2 = new TextBlock(); 
      tx2.Text = b; 

      tx1.SetValue(exGrid.ColumnProperty, 0); 
      tx2.SetValue(exGrid.ColumnProperty, 1); 

      this.Children.Add(tx1); 
      this.Children.Add(tx2); 
     } 
    } 
} 
관련 문제