0

与其对这个问题投反对票,不如回答告诉我出了什么问题!

我想在自定义控件的标签中设置按钮内容,因为当我使用它时,内容属性不可见并且按钮为空(无文本)。

Nocturno,谢谢,我是这个网站的新手,我把 C[space]sharp,但我的名声不允许我使用新标签,所以我删除了这个。

我想把Button.Content属性放在自定义控件的一个标签里,因为模板替换了原来的按钮设计,我用这个来节省代码,添加这个按钮不需要很多代码。

<Button x:Class="WpfApplication1.UserControl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="35" d:DesignWidth="273" Content="Button">
<Button.Template>
    <ControlTemplate>
        <Grid>
            <Rectangle Name="rGridBack" StrokeThickness="1">
                <Rectangle.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF4D4D4D" Offset="1" />
                        <GradientStop Color="#FF404040" Offset="0" />
                    </LinearGradientBrush>
                </Rectangle.Fill>
                <Rectangle.Stroke>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF4F4F4F" Offset="0" />
                        <GradientStop Color="#FF5B5B5B" Offset="1" />
                    </LinearGradientBrush>
                </Rectangle.Stroke>
            </Rectangle>
            <Rectangle Fill="#FF1E1E1E" Margin="1,1,1,1" Name="rThickness" />
            <Rectangle Margin="2,2,2,2" Name="rGridTop" StrokeThickness="1">
                <Rectangle.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF68686C" Offset="0" />
                        <GradientStop Color="#FF474747" Offset="1" />
                    </LinearGradientBrush>
                </Rectangle.Fill>
                <Rectangle.Stroke>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF7F7F7F" Offset="0" />
                        <GradientStop Color="#FF575757" Offset="1" />
                    </LinearGradientBrush>
                </Rectangle.Stroke>
            </Rectangle>
   <!--This label is where I want to set the Button.Content property-->
            <Label FontWeight="Normal" Foreground="White" HorizontalContentAlignment="Center" Name="tblckStep1Desc" Padding="0" VerticalContentAlignment="Center">
                <Label.Effect>
                    <DropShadowEffect BlurRadius="2" Color="Black" Direction="330" Opacity="0.7" ShadowDepth="1.5" />
                </Label.Effect>
            </Label>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Button.Content" Value="">
                <Setter Property="Content" TargetName="tblckStep1Desc">
                </Setter>
            </Trigger>
            <Trigger Property="UIElement.IsMouseOver" Value="True">
                <Setter Property="Shape.Fill" TargetName="rGridTop">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF838383" Offset="0" />
                            <GradientStop Color="#FF545454" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Stroke" TargetName="rGridTop">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF595959" Offset="1" />
                            <GradientStop Color="#FF929292" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Stroke" TargetName="rGridBack">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF414141" Offset="0" />
                            <GradientStop Color="#FF565656" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Fill" TargetName="rThickness">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF181818" Offset="1" />
                            <GradientStop Color="#FF181818" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="UIElement.IsEnabled" Value="False">
                <Setter Property="Shape.Fill" TargetName="rGridTop">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF68686C" Offset="0" />
                            <GradientStop Color="#FF474747" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Stroke" TargetName="rGridTop">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF7F7F7F" Offset="0" />
                            <GradientStop Color="#FF575757" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Stroke" TargetName="rGridBack">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF4F4F4F" Offset="0" />
                            <GradientStop Color="#FF5B5B5B" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Fill" TargetName="rThickness">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF1E1E1E" Offset="1" />
                            <GradientStop Color="#FF1E1E1E" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Control.Foreground" TargetName="tblckStep1Desc">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF898989" Offset="1" />
                            <GradientStop Color="#FF898989" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="ButtonBase.IsPressed" Value="True">
                <Setter Property="Shape.Fill" TargetName="rGridTop">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF313131" Offset="1" />
                            <GradientStop Color="#FF2E2E2E" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Stroke" TargetName="rGridTop">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF1F1F1F" Offset="1" />
                            <GradientStop Color="#FF1F1F1F" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Stroke" TargetName="rGridBack">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF414141" Offset="1" />
                            <GradientStop Color="#FF565656" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Shape.Fill" TargetName="rThickness">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF0C0C0C" Offset="1" />
                            <GradientStop Color="#FF0C0C0C" Offset="0" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Button.Template>

该按钮是基于 Adob​​e CS5 套件上的对话框形式,因为代码很多。

4

1 回答 1

1

您需要将 UserControls DataContext 设置为 RelativeSource=(RelativeSource Self}

<Button x:Class="WpfApplication1.UserControl1"
         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" 
         mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}}"
         d:DesignHeight="35" d:DesignWidth="273" Content="Button">

然后您需要将标签的绑定更改为:

<Label FontWeight="Normal" Content="{Binding Path=Content}" Foreground="White" HorizontalContentAlignment="Center" Name="tblckStep1Desc" Padding="0"   VerticalContentAlignment="Center">
    <Label.Effect>
        <DropShadowEffect BlurRadius="2" Color="Black" Direction="330" Opacity="0.7" ShadowDepth="1.5" />
    </Label.Effect>
</Label>

这将给出如下所示的结果:

在此处输入图像描述

于 2012-12-06T05:09:56.450 回答