我正在使用TabControl创建一个WPF应用程序(UI),其中包含四个TabItem。 My Application - 从User Tab,我想以某种方式(可能带有复选框或任何其他方式)选择哪些GridColumns将显示在用户选项卡上。我可以使用其他标签,但有时我需要让用户有机会只使用他/她想要的特定输出。我怎样才能做到这一点?我是C#和wpf的新手,所以如果你能解释一个简单的解决方案并提供一些代码,我会赞同它。
答案 0 :(得分:0)
在回答你的问题之前,请给你一个简短的提示:当你提出要求时,发布一些代码,否则很难有人花时间帮助你。
要实施您的申请,您需要考虑:
ColumnDefinitions
属性要解决第2点,您可以阅读这个非常有趣的article by Josh Smith。
他实际上创造了一种特殊的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();
}
}
当然这只是一个示例原型,但我相信它可以帮助您完成应用程序。