0

DependencyPropertyカスタムボタン用に作成しました。プロパティはと呼ばれIsCheckedます。IsChecked == trueボタンの背景色を別の色に変更し、この色を。まで保持する場合IsChecked == false

これが私のDependencyProperty

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainMenuButton), new PropertyMetadata(false));

    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

次にControlTemplate、このボタン用にあります:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ControlTemplate x:Key="MainMenuButtonStyle" TargetType="{x:Type UserControl}">
    <ControlTemplate.Resources>
                /* Some storyboards */
    </ControlTemplate.Resources>
    <Grid x:Name="grid">
        <Grid.Background>
            <SolidColorBrush Color="{DynamicResource MainUI_MainMenuButtonBackground}"/>
        </Grid.Background>
    </Grid>
    <ControlTemplate.Triggers>
                /* Some triggers */
    </ControlTemplate.Triggers>
</ControlTemplate>

私の問題は、どのようにアクセスできIsChecked、その現在の値に基づいて背景を変更できるgridかということです。少し前にそれを試しただけで完全に失敗する前にそれをしていません。

前もって感謝します :)

編集:これUserControlも同様です:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="IFCSMainInterface.MainMenuButton"
x:Name="UserControl"                            
d:DesignWidth="640" d:DesignHeight="480" Width="272" Height="110" Template="{DynamicResource MainMenuButtonStyle}">

<Grid x:Name="LayoutRoot"/>

4

1 に答える 1

1

問題はここにあると思います:

<ControlTemplate x:Key="MainMenuButtonStyle" TargetType="{x:Type UserControl}">

依存関係プロパティを使用するには、正しい型を設定する必要があります。ResourceDictionary にクラスの名前空間を書き込み、型をコントロール テンプレートに正しく配置します。次に例を示します。

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:my="clr-namespace:ProjectName.NameSpace">
    <ControlTemplate TargetType="my:MainMenuButton" x:Key="MainMenuButtonStyle"> 
          <!-- ... -->
    </ControlTemplate>
 </ResourceDictionary>

編集(説明されました):

xaml 独自のコントロールでテンプレートを定義しようとしていますが、これは適切ではないようです。Generic.xaml を使用するコントロールを作成するなど、他のアプローチを探すことをお勧めします。

( http://utahdnug.org/blogs/xamlcoder/archive/2007/12/13/building-custom-template-able-wpf-controls.aspx )

ただし、依存関係プロパティを (現在の方法で) 使用したい場合は、次のコードを試すことができます (たとえば)。

<UserControl x:Class="SamplesStack.Controls.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:my="clr-namespace:SamplesStack.Controls">
<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Grid x:Name="LayoutRoot"> 
            <ContentPresenter />
        </Grid>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="True">
                <Setter TargetName="LayoutRoot" Property="Background" Value="Red"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</UserControl.Template>

これにより、コントロールが期待どおりに機能します。とてもかっこいいとは思いませんが、DataTrigger を使用してください。

于 2012-09-22T17:15:39.340 に答える