1

我创建了一个 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>

请帮忙。

4

2 回答 2

2

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

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

并以您的风格将其设置TemplateBinding为您的标签内容

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

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

So if you don't need any additional functionality there is no need to create your own "PoupContentControl" because the built in HeaderedContentControl gives you the ability to use a Header in your Style:

A sample with using

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

And in your stlye change the TargetType to HeaderedContentControl and modify your Label to:

<Label Name="popupTitle" Content="{TemplateBinding Header}" ... />  
于 2012-08-21T06:38:12.253 回答
1

The proper way to do this is making a custom contentcontrol and adding a "popupHeader" dependencyproperty to that class. That way, you can bind your header to that property straight away and you can still use your tag for other purposes.

Make a class like PopUpHeader that inherits from contentcontrol and add a dependencyproperty to it.

Create a class like this:

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); }
    }
}

To be able to reference this in your XAML, input this namespace:

xmlns:local="clr-namespace:YourNamespace"

where "YourNamespace" is the namespace you have put the above class in.

Then change some targettype attributes in your style:

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

and also this line

<ControlTemplate TargetType="local:PopUpHelper">

change the binding of the content of your label to:

Content="{TemplateBinding PopUpHeader}"

You can use your new contentcontrol in your xaml like this:

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

If this would fail, check out the sample app I made for this: download

于 2012-08-21T07:07:19.773 回答