1

私は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 プロパティを

  1. ホスティング ボタンの前景色と同じ
  2. ボタンが押されたり無効になったりすると、それに応じて変更されます

どうすればそれを達成できますか?


私のあまり良くない解決策

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 が数ミリ秒の遅延で更新されることです。 それはよく見えないので、より良い解決策を探しています

4

1 に答える 1

0

これはSL5またはWPFで機能します

Buttonの代わりにに移動すると、相対ソースアプローチで問題なく機能しContentPresenterます。

Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}"

他のタイプのボタンなどをAncestorType=ButtonBase操作するために同じテンプレートが必要かどうかを定義することもできます。RepeatButtonToggleButton

編集:

これはSL3/4またはWP7のハックです

TextBlockあなたにを入れて、DataTemplateその前景にバインドします。

<DataTemplate>
    <Grid>
        <TextBlock x:Name="ForegroundProvider" Width="0" Height="0" Opacity="0"/>
        <Path Fill="{Binding Foreground, ElementName=ForegroundProvider}" ..../>
    </Grid>
</DataTemplate>

醜いですが、それはWP7です... :(

于 2013-01-10T10:37:17.430 に答える