我正在做类似于WPF User control 中的 Multiple Content Presenters 的事情。
我正在为条件 if 语句创建一个可视化小部件,并且我希望同时存在真假内容。它与原始帖子的不同之处在于我希望能够在真假内容中有多个项目。虽然我可以绑定 ContentPresenter 的 Content 属性,但 ItemsPresenter 没有类似的东西可以绑定。
有没有办法可以在控件中构建(不仅仅是让调用 XAML 将其包装在 Grid/StackPanel/等中)?
public class ConditionalBlock : Control
{
static ConditionalBlock ()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ConditionalBlock), new FrameworkPropertyMetadata(typeof(ConditionalBlock)));
}
public ConditionalBlock ()
{
Background = new SolidColorBrush(Color.FromRgb(0x45, 0x0F, 0x45));
}
public static readonly DependencyProperty LeftWidthProperty = DependencyProperty.Register("LeftWidth", typeof(double), typeof(ConditionalBlock), new UIPropertyMetadata(20.0));
public double LeftWidth
{
get => (double) GetValue(LeftWidthProperty);
set => SetValue(LeftWidthProperty, value);
}
public static readonly DependencyProperty BottomHeightProperty = DependencyProperty.Register("BottomHeight", typeof(double), typeof(ConditionalBlock), new UIPropertyMetadata(10.0));
public double BottomHeight
{
get => (double) GetValue(BottomHeightProperty);
set => SetValue(BottomHeightProperty, value);
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ConditionalBlock), new UIPropertyMetadata("If"));
public string Title
{
get => (string) GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public static readonly DependencyProperty HasSettingsProperty = DependencyProperty.Register("HasSettings", typeof(bool), typeof(ConditionalBlock), new UIPropertyMetadata(true));
public bool HasSettings
{
get => (bool) GetValue(HasSettingsProperty);
set => SetValue(HasSettingsProperty, value);
}
public static readonly DependencyProperty TrueTitleProperty = DependencyProperty.Register("TrueTitle", typeof(string), typeof(ConditionalBlock), new UIPropertyMetadata("True"));
public string TrueTitle
{
get => (string) GetValue(TrueTitleProperty);
set => SetValue(TrueTitleProperty, value);
}
public static readonly DependencyProperty TrueContentProperty = DependencyProperty.Register("TrueContent", typeof(object), typeof(ConditionalBlock), null);
public object TrueContent
{
get => GetValue(TrueContentProperty);
set => SetValue(TrueContentProperty, value);
}
public static readonly DependencyPropertyKey HasTrueItemsProperty = DependencyProperty.RegisterReadOnly("HasTrueItems", typeof(bool), typeof(ConditionalBlock), new UIPropertyMetadata(false));
public bool HasTrueItmes => TrueContent != null;
public static readonly DependencyProperty ElseHeightProperty = DependencyProperty.Register("ElseHeight", typeof(double), typeof(ConditionalBlock), new UIPropertyMetadata(10.0));
public double ElseHeight
{
get => (double) GetValue(ElseHeightProperty);
set => SetValue(ElseHeightProperty, value);
}
public static readonly DependencyProperty FalseTitleProperty = DependencyProperty.Register("FalseTitle", typeof(string), typeof(ConditionalBlock), new UIPropertyMetadata("False"));
public string FalseTitle
{
get => (string) GetValue(FalseTitleProperty);
set => SetValue(FalseTitleProperty, value);
}
public static readonly DependencyProperty FalseContentProperty = DependencyProperty.Register("FalseContent", typeof(object), typeof(ConditionalBlock), null);
public object FalseContent
{
get => GetValue(FalseContentProperty);
set => SetValue(FalseContentProperty, value);
}
public static readonly DependencyPropertyKey HasFalseItemsProperty = DependencyProperty.RegisterReadOnly("HasFalseItems", typeof(bool), typeof(ConditionalBlock), new UIPropertyMetadata(false));
public bool HasFalseItmes => FalseContent != null;
}
<Style TargetType="{x:Type local:ConditionalBlock}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ConditionalBlock}">
<Grid Background="White"
Margin="0,1">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="3" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--This is the top bar-->
<Border Grid.Row="0"
Grid.ColumnSpan="3"
CornerRadius="4"
Background="{TemplateBinding Background}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Margin="7,0,0,0"
VerticalAlignment="Center"
Text="{TemplateBinding Title}"
Foreground="White" />
<Image
Grid.Column="1"
Source="{StaticResource SettingsIcon}"
Visibility="{TemplateBinding HasSettings, Converter={StaticResource BooleanVisibilityConverter}}"
VerticalAlignment="Center"
MaxWidth="10"
Margin="0,1,5,0"
Stretch="Uniform" />
</Grid>
</Border>
<!--This is the true block-->
<Grid
Grid.Row="1"
Grid.Column="0"
Width="{TemplateBinding LeftWidth}"
Visibility="{TemplateBinding HasTrueItems, Converter={StaticResource BooleanVisibilityConverter}}"
Margin="0,-3.5"
Background="{TemplateBinding Background}">
<TextBlock
VerticalAlignment="Center"
Text="{TemplateBinding TrueTitle}"
Foreground="White">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90"></RotateTransform>
</TextBlock.LayoutTransform>
</TextBlock>
</Grid>
<Grid
Grid.Row="1"
Grid.Column="1"
Visibility="{TemplateBinding HasTrueItems, Converter={StaticResource BooleanVisibilityConverter}}"
Margin="-1"
Background="{TemplateBinding Background}">
<Border Margin="1,1,-1,1"
CornerRadius="2,0,0,2"
Background="White" />
</Grid>
<Grid
Grid.Row="1"
Grid.Column="2"
Visibility="{TemplateBinding HasTrueItems, Converter={StaticResource BooleanVisibilityConverter}}"
Margin="0,0,0,1">
<ContentPresenter Content="{TemplateBinding TrueContent}" />
</Grid>
<!--This is the else bar-->
<Border Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="3"
Height="{TemplateBinding ElseHeight}"
CornerRadius="4"
Background="{TemplateBinding Background}" />
<!--This is the false block-->
<Grid
Grid.Row="3"
Grid.Column="0"
Width="{TemplateBinding LeftWidth}"
Visibility="{TemplateBinding HasFalseItems, Converter={StaticResource BooleanVisibilityConverter}}"
Margin="0,-3.5"
Background="{TemplateBinding Background}">
<TextBlock VerticalAlignment="Center"
Text="{TemplateBinding FalseTitle}"
Foreground="White">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90"></RotateTransform>
</TextBlock.LayoutTransform>
</TextBlock>
</Grid>
<Grid
Grid.Row="3"
Grid.Column="1"
Visibility="{TemplateBinding HasFalseItems, Converter={StaticResource BooleanVisibilityConverter}}"
Margin="-1"
Background="{TemplateBinding Background}">
<Border Margin="1,1,-1,1"
CornerRadius="2,0,0,2"
Background="White" />
</Grid>
<Grid
Grid.Row="3"
Grid.Column="2"
Visibility="{TemplateBinding HasFalseItems, Converter={StaticResource BooleanVisibilityConverter}}"
Margin="0,0,0,1">
<ContentPresenter Content="{TemplateBinding FalseContent}" />
</Grid>
<!--This is the bottom bar-->
<Border Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="3"
Height="{TemplateBinding BottomHeight}"
CornerRadius="4"
Background="{TemplateBinding Background}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>