在样式控件上设置标题

时间:2012-08-21 06:27:33

标签: .net wpf popup styles

我创建了一个WPF弹出样式,并在应用程序的许多地方使用它。 弹出标题是在样式中定义的,我不知道如何在应用程序中将其更改为不同的值,这就是样式:

<Style x:Key="PopupContentStyle1" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Grid Height="90" Width="392" Background="Transparent">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.092*"></ColumnDefinition>
                            <ColumnDefinition Width="0.007*"/>
                            <ColumnDefinition Width="0.054*"/>
                            <ColumnDefinition Width="0.115*"/>
                            <ColumnDefinition Width="0.732*"/>
                        </Grid.ColumnDefinitions>
                        <Border CornerRadius="10" Grid.ColumnSpan="5">
                            <Border.Background>
                                <LinearGradientBrush
                            EndPoint="0.5,1"
                            StartPoint="0.5,0">
                                    <GradientStop Color="#FF333C3C"
                                Offset="0" />
                                    <GradientStop Color="#FF464646"
                                Offset="0.25" />
                                    <GradientStop Color="#FF504E50"
                                Offset="0.75" />
                                    <GradientStop Color="#FF595D59"
                                Offset="1" />
                                </LinearGradientBrush>
                            </Border.Background>

                            <Border Background="{DynamicResource TemplateBackgroundColour}" Margin="5,15,5,5" CornerRadius="5" BorderThickness="0,1,0,0">
                                <ContentPresenter />
                            </Border>
                        </Border>
                        <Label Name="popupTitle" Content="Sample title" Grid.Column="0" HorizontalAlignment="Center" Height="Auto" Margin="0" VerticalAlignment="Top" Width="Auto" Foreground="{DynamicResource DefaultFontColour}" Padding="0" Grid.ColumnSpan="5" />

                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这就是我打电话的方式,并希望更改标题以反映弹出内容:

<Popup x:Name="p1" AllowsTransparency="True">
        <ContentControl Style="{StaticResource PopupContentStyle1}" >
            <ContentControl.Content>
                <Grid>
                    <TextBox>Popup user control goes here</TextBox>

                </Grid>
            </ContentControl.Content>

        </ContentControl>
    </Popup>

请帮忙。

2 个答案:

答案 0 :(得分:2)

您可以(错误?)使用Tag属性将标题存储在ContentControl

<ContentControl Style="{StaticResource PopupContentStyle1}" Tag="Sample Title" >
 ...
</ContentControl>

在您的样式中,将TemplateBinding设置为您的标签内容

<Label Name="popupTitle" Content="{TemplateBinding Tag}" ... />

或者作为替代解决方案,您可以使用具有内置Header属性的HeaderedContentControl,您可以将其用作弹出标题。

因此,如果您不需要任何其他功能,则无需创建自己的“PoupContentControl”,因为内置的HeaderedContentControl使您能够在样式中使用Header:< / p>

使用

的示例
<Popup x:Name="p1" AllowsTransparency="True" IsOpen="True" >
    <HeaderedContentControl Style="{StaticResource PopupContentStyle1}" 
                            Header="Sample title"> 
         <HeaderedContentControl.Content>
             <Grid>
                 <TextBox>Popup user control goes here</TextBox>
             </Grid>
         </HeaderedContentControl.Content>
     </HeaderedContentControl>
</Popup>

在您的身份中,将TargetType更改为HeaderedContentControl并将您的Label修改为:

<Label Name="popupTitle" Content="{TemplateBinding Header}" ... />  

答案 1 :(得分:1)

执行此操作的正确方法是进行自定义内容控制并向该类添加“popupHeader”依赖项属性。这样,您可以立即将标题绑定到该属性,您仍然可以将标记用于其他目的。

创建一个类似PopUpHeader的类,它继承自contentcontrol并为其添加dependencyproperty。

创建一个这样的类:

public class PopUpHelper : ContentControl
{
    public static readonly DependencyProperty PopUpHeaderProperty = DependencyProperty.Register("PopUpHeader", typeof(String), typeof(PopUpHelper), null);
    public string PopUpHeader
    {
        get { return Convert.ToString(GetValue(PopUpHeaderProperty)); }
        set { SetValue(PopUpHeaderProperty, value); }
    }
}

为了能够在您的XAML中引用它,请输入以下命名空间:

的xmlns:本地= “CLR-名称空间:YourNamespace”

其中“YourNamespace”是您将上述类放入的命名空间。

然后更改样式中的某些targettype属性:

<Style x:Key="PopupContentStyle1" TargetType="local:PopUpHelper">

也是这一行

<ControlTemplate TargetType="local:PopUpHelper">

将标签内容的绑定更改为:

Content="{TemplateBinding PopUpHeader}"

您可以在xaml中使用新的contentcontrol,如下所示:

<local:PopUpHelper PopUpHeader="This is the header" Style="{StaticResource PopupContentStyle1}">
    <local:PopUpHelper.Content>
        <Grid>
            <TextBlock Text="Here goes content"/>
        </Grid>
    </local:PopUpHelper.Content>
</local:PopUpHelper>

如果这会失败,请查看我为此制作的示例应用:download