2016-11-08 48 views
1

我正在創建一個WPF應用程序(UI),其中包含四個TabItems的TabControl。 My Application - 從User Tab開始,我想以某種方式選擇(也許通過複選框或其他方式)GridColumns中的哪個將顯示在用戶選項卡上。我可以與其他選項卡一起工作,但有時我需要給用戶提供僅使用他/她想要的特定輸出的機會。我該如何做這項工作?我是C#和wpf的新手,所以如果你能解釋一個簡單的解決方案並提供一些代碼,我會對它進行應用。如何從某些選項卡中選擇網格列並將它們顯示在另一個選項卡中?

回答

0

在回答你的問題之前,給你一個簡短的提示:當你問,發佈一些代碼時,否則很難有人花時間幫助你。

爲了實現你的應用程序,你需要考慮:

  1. ElementName data binding
  2. 的ElementName數據綁定可以在ColumnDefinitions財產

不起作用爲了解決這個點數2,你可以閱讀非常有趣article by Josh Smith

Pratically他創造了一種特殊的ElementSpy帶有附加屬性:

public class ElementSpy : Freezable 
{ 
    private DependencyObject element; 

    public static ElementSpy GetNameScopeSource(DependencyObject obj) 
    { 
     return (ElementSpy)obj.GetValue(NameScopeSourceProperty); 
    } 

    public static void SetNameScopeSource(DependencyObject obj, ElementSpy value) 
    { 
     obj.SetValue(NameScopeSourceProperty, value); 
    } 

    public static readonly DependencyProperty NameScopeSourceProperty = 
     DependencyProperty.RegisterAttached("NameScopeSource", typeof(ElementSpy), typeof(ElementSpy), 
      new UIPropertyMetadata(null, OnNameScopeSourceProperty)); 

    private static void OnNameScopeSourceProperty(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    { 
     INameScope nameScope; 
     ElementSpy elementSpy = args.NewValue as ElementSpy; 

     if (elementSpy != null && elementSpy.Element != null) 
     { 
      nameScope = NameScope.GetNameScope(elementSpy.Element); 
      if (nameScope != null) 
      { 
       d.Dispatcher.BeginInvoke(new Action<DependencyObject, INameScope>(SetScope), 
        System.Windows.Threading.DispatcherPriority.Normal, 
        d, nameScope); 
      } 
     } 
    } 

    private static void SetScope(DependencyObject d, INameScope nameScope) 
    { 
     NameScope.SetNameScope(d, nameScope); 
    } 

    public DependencyObject Element 
    { 
     get 
     { 
      if (element == null) 
      { 
       PropertyInfo propertyInfo = typeof(Freezable).GetProperty("InheritanceContext", 
        BindingFlags.NonPublic | BindingFlags.Instance); 

       element = propertyInfo.GetValue(this, null) as DependencyObject; 

       if (element != null) 
       { 
        Freeze(); 
       } 
      } 

      return element; 
     } 
    } 

    protected override Freezable CreateInstanceCore() 
    { 
     return new ElementSpy(); 
    } 
} 

現在我們可以使用綁定在我們的XAML

<Window.Resources> 
    <local:BooleanWidthConverter x:Key="BooleanWidthConverter" /> 
    <local:ElementSpy x:Key="ElementSpy" /> 
</Window.Resources> 

    <StackPanel HorizontalAlignment="Stretch"> 
    <Grid local:ElementSpy.NameScopeSource="{StaticResource ElementSpy}" 
      Margin="0,0,0,30"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="{Binding ElementName=cb1, Path=IsChecked, Converter={StaticResource BooleanWidthConverter}, Mode=OneWay}" /> 
      <ColumnDefinition Width="{Binding ElementName=cb2, Path=IsChecked, Converter={StaticResource BooleanWidthConverter}, Mode=OneWay}" /> 
      <ColumnDefinition Width="{Binding ElementName=cb3, Path=IsChecked, Converter={StaticResource BooleanWidthConverter}, Mode=OneWay}" /> 
     </Grid.ColumnDefinitions> 

     <TextBlock HorizontalAlignment="Center" Text="Column 1" Margin="4" Grid.Column="0" /> 
     <TextBlock HorizontalAlignment="Center" Text="Column 2" Margin="4" Grid.Column="1" /> 
     <TextBlock HorizontalAlignment="Center" Text="Column 3" Margin="4" Grid.Column="2" /> 

    </Grid> 

    <CheckBox x:Name="cb1" Content="Column 1" Margin="4" IsChecked="true" HorizontalAlignment="Center" /> 
    <CheckBox x:Name="cb2" Content="Column 2" Margin="4" IsChecked="true" HorizontalAlignment="Center" /> 
    <CheckBox x:Name="cb3" Content="Column 3" Margin="4" IsChecked="true" HorizontalAlignment="Center" /> 
</StackPanel> 

爲了完成我們的代碼,我們需要一個簡單的轉換:

public class BooleanWidthConverter : IValueConverter 
{ 
    private static GridLength star = new GridLength(1, GridUnitType.Star); 
    private static GridLength zero = new GridLength(0, GridUnitType.Pixel); 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool boolValue = (bool)value; 

     return boolValue ? star : zero; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

當然這只是一個樣本原型,但我確定它可以幫助你與你的應用程序。

相關問題