0

I have a toolbar that show many Button with Image and TextBlock.

Then, I create a CommandManager to manager all Button's commands. So, I want to implement a factory to assign action by Button's TextBlock.

This is my xaml and Command function, I try to pass all Button's Text, but I don't know how to do.

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Command" Value="{Binding ActionCommand}"/>
            <Setter Property="CommandParameter" Value="{Binding Path=Text}"/>
        </Style>
    </StackPanel.Resources>
    <Button>
        <StackPanel>
            <Image Source="/Resources/ToolBar/open.png"/>
            <TextBlock Text="Open"/>
        </StackPanel>
    </Button>
    <Button>
        <StackPanel>
            <Image Source="/Resources/ToolBar/save.png"/>
            <TextBlock Text="Save"/>
        </StackPanel>
    </Button>
</StackPanel>

ActionManager:

public ICommand ActionCommand { get { return new RelayCommand(_onActionCommand); } }

private void _onActionCommand(object parameter)
{
    if (parameter == null)
    {
        return;
    }

    string buttonContent = parameter as string;
    switch (buttonContent)
    {
        case "Open":
            new OpenWindow().ShowDialog();
            break;
        case "Open":
            new Save();
            break;
    }
}
4

3 回答 3

1

AButton没有“文本”的概念,但您可以将Tag属性设置为 astring并传递这个:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Command" Value="{Binding ActionCommand}"/>
            <Setter Property="CommandParameter" Value="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"/>
        </Style>
    </StackPanel.Resources>
    <Button Tag="Open">
        <StackPanel>
            <Image Source="/Resources/ToolBar/open.png"/>
            <TextBlock Text="Open"/>
        </StackPanel>
    </Button>
    <Button Tag="Save">
        <StackPanel>
            <Image Source="/Resources/ToolBar/save.png"/>
            <TextBlock Text="Save"/>
        </StackPanel>
    </Button>
</StackPanel>
于 2017-05-19T10:38:50.400 回答
0

我会这样做:

<Button Tag="AnyUniqueString">

并将命令绑​​定到它

<Setter Property="CommandParameter" Value="{Binding Path=Tag}"/>

所以你没有链接到文本按钮(你可以翻译它) - 但要注意,使用一个命令,你将无法管理刷新和 can_execute ,这将启用/禁用你的按钮。将每个按钮绑定到单独的命令可能并不复杂......?

我在我的解决方案CBR中这样使用它

<StackPanel Margin="10,0,10,0" Orientation="Vertical">
                <Button Style="{DynamicResource CbrStandardButton}" Margin="2,10,2,10"
                        Command="{Binding ForwardCommand}" CommandParameter="CatalogNewCommand" >
                    <DockPanel Margin="10">
                        <Image Source="/CBR;component/Resources/Images/32x32/library_new.png" Width="32"></Image>
                        <Label Style="{DynamicResource CbrLabel}"
                               Content="{LocalizationExtension ResModul=CBR, Key=HomeView.LblActionNew, DefaultValue=Start a new library}" />
                    </DockPanel>
                </Button>
                <Button Style="{DynamicResource CbrStandardButton}" Margin="2,10,2,10"
                        Command="{Binding ForwardCommand}" CommandParameter="BookOpenCommand" >
                    <DockPanel Margin="10">
                        <Image Source="/CBR;component/Resources/Images/32x32/book/book_read.png" Width="32"></Image>
                        <Label Style="{DynamicResource CbrLabel}"
                               Content="{LocalizationExtension ResModul=CBR, Key=HomeView.LblActionRead, DefaultValue=Read a book}" />
                    </DockPanel>
                </Button>
                <Button Style="{DynamicResource CbrStandardButton}" Margin="2,10,2,10"
                        Command="{Binding ForwardCommand}" CommandParameter="SysHelpCommand">
                    <DockPanel Margin="10">
                        <Image Source="/CBR;component/Resources/Images/32x32/book_type/book_type_xps.png" Width="32"></Image>
                        <Label Style="{DynamicResource CbrLabel}"
                               Content ="{LocalizationExtension ResModul=CBR, Key=HomeView.LblActionTutorial, DefaultValue=Quick start tutorial}" />
                    </DockPanel>
                </Button>
            </StackPanel>
于 2017-05-19T06:37:28.937 回答
0

你可以使用这个标记扩展:

https://www.codeproject.com/Articles/456589/Bindable-Converter-Parameter

它允许您将命令参数绑定到 xaml 属性。因此,例如,您可以在按钮的标签字段中放置一些标识符,并使用扩展名将其绑定到命令参数。这样,您只使用一个命令,只使用一个绑定,并且您获得了区分命令处理程序中的按钮所需的一切。

希望能帮助到你

于 2017-05-19T07:11:25.053 回答