3

我有一个场景,我将相同的值传递给数据模板中不同按钮的多值转换器,这一切都很好。但是想知道通过删除可重复元素来使代码更好的方法,因为我将相同的内容传递给

<telerik:RadButton.Visibility>
    <MultiBinding Converter="{StaticResource multiValueToBooleanConverter}" ConverterParameter="ViewCommentsDialog">
        <Binding Path="DataContext.CanEdit" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
        <Binding Path="DataContext.CanView" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
        <Binding Path="Comment"/>
        <Binding Path="ExecutionId"/>
    </MultiBinding>
</telerik:RadButton.Visibility>

代码:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <telerik:RadButton Margin="2" Command="{Binding DataContext.ViewCommentCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}">
            <telerik:RadButton.Visibility>
                <MultiBinding Converter="{StaticResource multiValueToBooleanConverter}" ConverterParameter="ViewCommentsDialog">
                    <Binding Path="DataContext.CanEdit" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                    <Binding Path="DataContext.CanView" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                    <Binding Path="Comment"/>
                    <Binding Path="ExecutionId"/>
                </MultiBinding>
            </telerik:RadButton.Visibility>
            <telerik:RadButton.Content>
                <Image Width="20" Height="20" Source="pack://application:,,,/Mizuho.Minar.UI.Common;component/png/Comments.png" />
            </telerik:RadButton.Content>
        </telerik:RadButton>
        <telerik:RadButton Margin="2" Command="{Binding  DataContext.AddCommentCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}">
            <telerik:RadButton.Visibility>
                <MultiBinding Converter="{StaticResource multiValueToBooleanConverter}" ConverterParameter="AddCommentDialog">
                    <Binding Path="DataContext.CanEdit" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                    <Binding Path="DataContext.CanView" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                    <Binding Path="Comment"/>
                    <Binding Path="ExecutionId"/>
                </MultiBinding>
            </telerik:RadButton.Visibility>
            <telerik:RadButton.Content>
                <Image Width="20" Height="20" Source="pack://application:,,,/Mizuho.Minar.UI.Common;component/png/Comment-Add.png" />
            </telerik:RadButton.Content>
        </telerik:RadButton>
        <telerik:RadButton Margin="2" Command="{Binding DataContext.ViewBreaksCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}">
            <telerik:RadButton.Visibility>
                <MultiBinding Converter="{StaticResource multiValueToBooleanConverter}" ConverterParameter="ViewBreaksDialog">
                    <Binding Path="DataContext.CanEdit" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                    <Binding Path="DataContext.CanView" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                    <Binding Path="Comment"/>
                    <Binding Path="ExecutionId"/>
                </MultiBinding>
            </telerik:RadButton.Visibility>
            <telerik:RadButton.Content>
                <Image Width="20" Height="20" Source="pack://application:,,,/Mizuho.Minar.UI.Common;component/png/Folder-Bug.png" />
            </telerik:RadButton.Content>
        </telerik:RadButton>
    </StackPanel>
</DataTemplate>
4

1 回答 1

2

基本上有两种非常简单的方法。

选项 1 - 绑定到其他属性。

您可以在其他一些依赖属性上设置您的绑定(实际上是多重绑定),然后将您的按钮可见性绑定到该属性。如果您没有此类属性的任何好的候选者,您可以使用一些简单的对象,该对象具有放置在 Resources 集合中的单个依赖项属性。这是此类对象的类代码:

public class VisibilityValueHolder : ValueHolder<Visibility> { }

public abstract class ValueHolder<T> : DependencyObject
{
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
        "Value", typeof(T), typeof(ValueHolder<T>), null);
    public T Value
    {
        get { return (T)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public ValueHolder() { }

    public ValueHolder(T value)
    {
        Value = value;
    }
}

以及代码中的用法:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <common:VisibilityValueHolder x:Key="VisibilityHolder">
                <common:VisibilityValueHolder.Value>
                     <MultiBinding Converter="{StaticResource multiValueToBooleanConverter}" ConverterParameter="ViewCommentsDialog">
                        <Binding Path="DataContext.CanEdit" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                        <Binding Path="DataContext.CanView" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                        <Binding Path="Comment"/>
                        <Binding Path="ExecutionId"/>
                    </MultiBinding>
                </common:VisibilityValueHolder.Value>
            </common:VisibilityValueHolder.Value>
        </StackPanel.Resources>

        <telerik:RadButton Margin="2" Command="{Binding DataContext.ViewCommentCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}"
                Visibility={Binding Value, Source={StaticResource VisibilityHolder}}>
            <telerik:RadButton.Content>
                <Image Width="20" Height="20" Source="pack://application:,,,/Mizuho.Minar.UI.Common;component/png/Comments.png" />
            </telerik:RadButton.Content>
        </telerik:RadButton>

选项 2 - 在 Setter 中使用带有绑定的样式

您可以声明隐式或显式样式(如果您想对所有按钮应用相同的可见性,或者只对选定的按钮应用相同的可见性)。

您的代码如下所示:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <Style TargetType="telerik:RadButton">
                <Setter Property="Visibility">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource multiValueToBooleanConverter}" ConverterParameter="ViewCommentsDialog">
                            <Binding Path="DataContext.CanEdit" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                            <Binding Path="DataContext.CanView" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                            <Binding Path="Comment"/>
                            <Binding Path="ExecutionId"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>

        <telerik:RadButton Margin="2" Command="{Binding DataContext.ViewCommentCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}">
            <telerik:RadButton.Content>
                <Image Width="20" Height="20" Source="pack://application:,,,/Mizuho.Minar.UI.Common;component/png/Comments.png" />
            </telerik:RadButton.Content>
        </telerik:RadButton>
于 2014-05-29T14:32:05.797 回答