2016-05-06 6 views
1

CommandParameterが共通するボタンがいくつかありますが、ボタンが多いほど乱雑です。WPFの子要素に 'CommandParameter'を渡すには

<StackPanel> 
    <Button Command="{Binding Foo1Command}" CommandParameter="{Binding CommonParam, Source={StaticResource CommonObj}}" /> 
    <Button Command="{Binding Foo2Command}" CommandParameter="{Binding CommonParam, Source={StaticResource CommonObj}}" /> 
    <Button Command="{Binding Foo3Command}" CommandParameter="{Binding CommonParam, Source={StaticResource CommonObj}}" /> 
    ... 
    ... 
    ... 
</StackPanel> 


私は最終的には以下のように上記のコードを作りたい、子要素まで親要素にCommandParameterを渡したい、そう。

<StackPanel CommandParameter="{Binding CommonParam, Source={StaticResource CommonObj}}"> 
    <Button Command="{Binding Foo1Command}" /> 
    <Button Command="{Binding Foo2Command}" /> 
    <Button Command="{Binding Foo3Command}" /> 
    ... 
    ... 
    ... 
</StackPanel> 


それは可能ですか?もしそうなら、私はそれを達成するために何を学ぶべきかを提案すればとても感謝しています。

答えて

3

が可能です。すべてのボタンは、デフォルトのパラメータが設定されている共通のスタイルを共有できます。

はそれがない

<StackPanel> 
    <StackPanel.Resources> 
     <Style TargetType="Button"> 
      <Setter Property="CommandParameter" 
        Value="{Binding CommonParam, Source={StaticResource CommonObj}}"/> 
     </Style> 
    </StackPanel.Resources> 
    <Button Command="{Binding Foo1Command}" /> 
    <Button Command="{Binding Foo2Command}" /> 
    <Button Command="{Binding Foo3Command}" /> 
    <!--other code--> 
</StackPanel> 
+0

非常に巧妙なソリューション、1 –

+0

「ダウン子要素に親要素にCommandParameterを渡すと、」ああ、それはOKに動作しています。どうもありがとうございます! :D – jayeonsu

関連する問題