私はWP7プロジェクトで働いています。多数の ContentTemplates が定義されたスタイルのボタンがあります。これらの ContentTeplates には Shapes が含まれます (簡単にするために、Rectangle を使用します)。
<Button x:Name="button1"
Style="{StaticResource ButtonStyle1}"
ContentTemplate="{StaticResource DataTemplate1}">
</Button>
<Button x:Name="button2"
Style="{StaticResource ButtonStyle1}"
ContentTemplate="{StaticResource DataTemplate2}">
</Button>
...
<DataTemplate x:Key="DataTemplate1">
<Rectangle Height="100" Width="100" Fill="{QUESTION_HERE}"/>
</DataTemplate>
Rectangle の Fill プロパティを
- ホスティング ボタンの前景色と同じ
- ボタンが押されたり無効になったりすると、それに応じて変更されます
どうすればそれを達成できますか?
私のあまり良くない解決策
WinRT では、非常にうまく機能する RelativeSource アプローチを使用します。
Fill="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
残念ながら、WP では動作しません。その理由は、WP の ContentPresenter には ForegroundProperty がありませんが、WinRT にはあります。
そこで、ContentPresenter で独自の添付 DP を定義し、それを Button テンプレートで使用しようとしました。
Button.Template >> ContentControl.Template >>
<ContentPresenter
local:FrameworkElementExtensions.Foreground="{TemplateBinding Foreground}"
ContentPresenter RelativeSource バインディングで DP が機能し始めると定義すると、ご存知のとおりです。
しかし、悪い面は、ボタンが押されたときに、Rectangle の Fill が数ミリ秒の遅延で更新されることです。 それはよく見えないので、より良い解決策を探しています。