创建从用户代码/ xaml(UWP)接收内容的用户控件

时间:2018-12-06 21:50:40

标签: c# xaml uwp

我有一个用户控件,其中包含一个样式化的切换按钮。我想使用此控件并将内容传递给用户控件,如下所示:

用户控制:

<UserControl
    x:Class="TouchApp.UI.Views.DieselMotor.DieselToggle"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TouchApp.UI.Views.DieselMotor"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Name="DieselButton"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <UserControl.Resources>
        <Style x:Key="DieselToggleButton"  TargetType="ToggleButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid x:Name="RootGrid">
                            <Ellipse 
                                Margin="6,-49,0,0"
                                Width="340"
                                Height="340"
                                Stroke="Black"
                                StrokeThickness="2">
                                <Ellipse.Fill>
                                    <SolidColorBrush Color="{Binding Path=BackColor, ElementName=DieselButton}"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter 
                                Content="{TemplateBinding Content}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <ToggleButton 
        Style="{StaticResource DieselToggleButton}">
        <ContentControl Content="{TemplateBinding Content}"/>
    </ToggleButton>
</UserControl>

我希望可以如下使用用户控件:

<local:DieselToggle
    BackColor="Aqua"
    Command="{Binding SteeringCommand}" CommandParameter="{Binding Path=IsChecked, RelativeSource={RelativeSource Self}}">
    <StackPanel>
        <Image Source="../../../Resources/Diesel/Actuator_off.png"/>
        <TextBlock 
            Style="{StaticResource MainButtonText}"
            HorizontalTextAlignment="Center"
            x:Uid="buttonSteeringText"/>
    </StackPanel>
</local:DieselToggle>

但是结果是只显示了内容,但没有“传递”到切换按钮。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

我希望您遇到编译错误。 也许使用Binding可以解决

Style="{StaticResource DieselToggleButton}">
    <ContentControl Content="{Binding Content}"/>
</ToggleButton>