1

本当に厄介な問題に出くわしました。達成するのは簡単なことのように思えますが、解決策がわかりません。私がやりたいことは、ColorAnimation のToプロパティを MouseOverColor という DependencyProperty にバインドすることです。これは私のスタイルがどのように見えるかです:

<Style TargetType="{x:Type local:Button}">
  <Setter Property="Background" Value="{StaticResource SolidBlueDark}" />
  <Setter Property="BorderBrush" Value="{x:Null}" />
  <Setter Property="BorderThickness" Value="0" />
  <Setter Property="FocusVisualStyle" Value="{x:Null}" />
  <Setter Property="Foreground" Value="White" />
  <Setter Property="HorizontalContentAlignment" Value="Center" />
  <Setter Property="MouseOverColor" Value="Cyan" />
  <Setter Property="Padding" Value="1" />
  <Setter Property="VerticalContentAlignment" Value="Center" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:Button}">
        <Grid>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="CommonStates">
              <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0:0:0.4" />
              </VisualStateGroup.Transitions>
              <VisualState Name="Normal" />
              <VisualState Name="MouseOver">
                <Storyboard>
                  <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.Background).Color" To="{TemplateBinding MouseOverColor}" />
                </Storyboard>
              </VisualState>
              <VisualState Name="Pressed" />
              <VisualState Name="Disabled" />
            </VisualStateGroup>
            <VisualStateGroup Name="FocusStates">
              <VisualState Name="Unfocused" />
              <VisualState Name="Focused" />
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border 
            Name="Border" 
            Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            Height="{TemplateBinding Height}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
            Width="{TemplateBinding Width}" />
          <ContentPresenter 
            TextBlock.Foreground="{TemplateBinding Foreground}" 
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            Margin="{TemplateBinding Padding}" 
            RecognizesAccessKey="True" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

何が起こるかというと、何も起こらず、ボタンの色は同じままです。ColorAnimation 行を次のように変更すると、色が変わります。

<ColorAnimation 
  Storyboard.TargetName="Border"
  Storyboard.TargetProperty="(Border.Background).Color"
  To="Cyan" />

しかし、To color を MouseOverColor プロパティで設定し、ハードコーディングしないようにする必要があります。どうすればこれを実現できますか? あらゆる種類のバインディングを試しましたが、結果は常に同じです。

前もって感謝します。

編集:静的リソースを使用しても機能しますが、それは私が望むものではありません。私がやりたいことをする方法は本当にありませんか?使用できるようにしたい「マウスオーバー」の色ごとに新しいスタイルを作成してハードコードするのは意味がありません。

4

2 に答える 2

2

この MSDN フォーラム スレッドの Microsoft の作成者によると、あなたがしようとしていることは WPF ではサポートされていません。アニメーションの値は、動的またはバインドできないように固定する必要があります。私はちょうど同じ制限に出くわしました。実行しようとしてもエラーはスローされません。色が設定されないだけです。

于 2016-06-17T22:44:11.283 に答える
0

それがあなたのケースの解決策であるかどうかはわかりません...

MouseOverColor独自のカスタム プロパティの場合は、別の方法でTemplated Binding作業してみてください。

{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MouseOverColor}  

ここでもう少し詳しく説明しました

Image.Height TemplateBinding が機能しない

于 2013-04-07T11:36:10.057 に答える