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}" ... />

または、代替ソリューションとして、ポップアップ タイトルとして使用できるプロパティが組み込まれているHeaderedContentControlを使用できます。Header

したがって、追加の機能が必要ない場合は、独自の「PoupContentControl」を作成する必要はありません。ビルトインにより、スタイルでHeaderedContentControla を使用できるようになるためです。Header

使用例

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

そして、あなたのスタイルで to を変更し、TargetTypetoHeaderedContentControlを変更Labelします:

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

これを行う適切な方法は、カスタム コンテンツ コントロールを作成し、そのクラスに「popupHeader」依存プロパティを追加することです。そうすれば、ヘッダーをそのプロパティにすぐにバインドでき、タグを他の目的に引き続き使用できます。

contentcontrol を継承する PopUpHeader のようなクラスを作成し、それに依存プロパティを追加します。

次のようなクラスを作成します。

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:local="clr-namespace: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>

これが失敗する場合は、このために作成したサンプル アプリをチェックしてください:ダウンロード

于 2012-08-21T07:07:19.773 に答える