我有
UserControl
,它必须作为底切(我不知道它是如何正确命名的。当用户单击标题时,它显示其内容,并在用户再次单击时隐藏内容)
现在我有了下一个 .xaml 文件:
底切.xaml
<UserControl x:Class="App.Undercut"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MonsterCreator"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Name="uc">
<Grid>
<!-- Header -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{Binding Title, ElementName=uc}" VerticalAlignment="Center" />
</Grid>
<!-- Content -->
<Grid>
<StackPanel Orientation="Vertical">
???
</StackPanel>
</Grid>
</Grid>
</UserControl>
所以,我希望这个 UserControl 作为
StackPanel
工作,我可以在其中设置 Content
(是 Content 属性吗?) 乘法。但我不知道如何让它像这样工作:
<l:Undercut>
<Button Content="Test 1"/>
<TextBox Text="Test 2"/>
<TextBlock Text="Test 3"/>
...
<l:CutomUserControl ExampleProperty="Test 4"/>
</l:Undercut>
我尝试使用
ContentControl
来完成此操作,然后出现编译错误“属性内容设置多次”
我尝试将
Content
属性绑定到 StackPanel
的 Children
属性,但它是只读的
我知道这个功能在论坛中使用时被称为“剧透”。
您可以使用对其
IsChecked
状态做出反应的按钮和触发器在 XAML 中实现此类显示-隐藏-切换。
<StackPanel>
<!-- Header -->
<ToggleButton x:Name="TextSwitchButton" VerticalAlignment="Center">
<!-- remove this whole style if you dont want the text to toggle -->
<ToggleButton.Style>
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="ToggleButton">
<Setter Property="Content" Value="Show Text!" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="Hide Text!" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
<!-- Content -->
<TextBlock Name="TextBlock1" Text="Now you can read this very secret text!">
<TextBlock.Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="TextBlock">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TextSwitchButton, Path=IsChecked, Mode=OneWay}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>