在标签中设置按钮内容(自定义控件)

时间:2012-12-06 04:30:39

标签: wpf text

不要投票否定这个问题,回答告诉我出了什么问题!

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

Nocturno,谢谢,我是这个网站的新手,我把C [空间]清晰,但我的声誉不允许我使用新标签,所以我删除了这个。

我想将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>

该按钮基于对话框表单上的Adobe CS5套件,因为代码很多。

1 个答案:

答案 0 :(得分: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的Binding更改为:

<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>

这将产生如下结果:

enter image description here