使用参数进行资源DataTemplate或样式绑定

时间:2017-09-20 07:57:26

标签: c# wpf xaml data-binding binding

我想知道是否可以设置"参数绑定"在资源字典文件中,并在UserControl或Window中设置此特定绑定。

我的XAML代码如下所示:

<Style x:Key="ArrowImageButtonStyle" TargetType="Button">
    <Setter Property="Height" Value="100" />
    <Setter Property="Width" Value="100" />
</Style>

<Style x:Key="ArrowUpImageButtonStyle" BasedOn="{StaticResource ArrowImageButtonStyle}" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Width="90" Height="90" Source="pack://application:,,,/Resources/img_arrowUp.png" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ArrowRightImageButtonStyle" BasedOn="{StaticResource ArrowImageButtonStyle}" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Width="90" Height="90" Source="pack://application:,,,/Resources/img_arrowRight.png" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想要实现的是两件事。

首先,在一个地方设置箭头向上,向左,......的以下按钮的宽度和高度会很好,所以像这样:

<Style x:Key="ArrowImageButtonStyle" TargetType="Button">
    <Setter Property="Height" Value="100" />
    <Setter Property="Width" Value="100" />
    <Setter Property="DataTemplate.Width" Value="90" />
    <Setter Property="DataTemplate.Height" Value="90" />
</Style>

<Style x:Key="ArrowUpImageButtonStyle" BasedOn="{StaticResource ArrowImageButtonStyle}" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Width="{Binding}" Height="{Binding}" Source="pack://application:,,,/Resources/img_arrowUp.png" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ArrowRightImageButtonStyle" BasedOn="{StaticResource ArrowImageButtonStyle}" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Width="{Binding}" Height="{Binding}" Source="pack://application:,,,/Resources/img_arrowRight.png" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

其次,我还可以将Image-source设置为UserControl / Window的参数吗?这就是我的意思:

<!-- Resource -->
<Style x:Key="ArrowImageButtonStyle" TargetType="Button">
    <Setter Property="Height" Value="100" />
    <Setter Property="Width" Value="100" />
    <Setter Property="DataTemplate.Width" Value="90" />
    <Setter Property="DataTemplate.Height" Value="90" />
</Style>

<Style x:Key="ArrowUpImageButtonStyle" BasedOn="{StaticResource ArrowImageButtonStyle}" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Width="{Binding}" Height="{Binding}" Source="{Binding}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ArrowRightImageButtonStyle" BasedOn="{StaticResource ArrowImageButtonStyle}" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Width="{Binding}" Height="{Binding}" Source="{Binding}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- UserControl / Window -->
<Button Style="{StaticResource ArrowUpImageButtonStyle, Source="pack://application:,,,/Resources/img_arrowUp.png"}" />

0 个答案:

没有答案