使用样式更改按钮工具提示 - WPF

时间:2018-05-05 18:28:55

标签: c# wpf

我有一个DataGrid,它载有一个具有属性bool IsAutomaticSell的对象列表。我需要在更改值时,更新该行按钮的工具提示。我有以下代码但它不起作用。 THX

View.xaml

<DataGridTextColumn Header="Code"
                    Binding="{Binding Code}" />
<DataGridTemplateColumn Header="Actions"
                        Width="150">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Center">
                <Button Command="{Binding DataContext.AutomaticSellCommand,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
                        CommandParameter="{Binding}"
                        Padding="10"
                        Margin="0,2,2,2">
                    <iconPacks:PackIconModern Kind="CurrencyDollar" />
                    <Button.Style>
                        <Style TargetType="{x:Type Button}"
                               BasedOn="{StaticResource AccentedSquareButtonStyle}">
                            <Setter Property="ToolTip"
                                    Value="DEFAULT_TOOLTIP" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsAutomaticSell, UpdateSourceTrigger=PropertyChanged}"
                                             Value="True">
                                    <Setter Property="ToolTip"
                                            Value="NEW_TOOLTIP" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

ViewModel.cs

public ICommand AutomaticSellCommand => _automaticSellCommand ??
                         (_automaticSellCommand = new RelayCommand<OrderStatusDataWrapper>(AutomaticSell));

private static void AutomaticSell(OrderStatusDataWrapper orderStatusData)
{
    orderStatusData.IsAutomaticSell = !orderStatusData.IsAutomaticSell;
}

2 个答案:

答案 0 :(得分:0)

绑定工具提示。

XAML:

 <Button ToolTip={Binding ToolTip}" ... />

视图模型:

public string ToolTip => (IsAutomaticSell) ? "DEFAULT_TOOLTIP" : "NEW_TOOLTIP";

答案 1 :(得分:0)

您需要实现ValueConverter才能使其正常工作。

ValueConverter允许您根据布尔值处理要显示的内容。

MSDN

public class BoolToContentConverter : IValueConverter
{
    public BoolToContentConverter()
    {
        TrueContent = "True Tool Tip";
        FalseContent = "False Tool Tip";
        NullContent = "No Value";
    }

    public object TrueContent { get; set; }
    public object FalseContent { get; set; }
    public object NullContent { get; set; }

    public object Convert(object value, Type targetType, 
           object parameter, CultureInfo culture)
    {
        if (value == null)
            return NullContent;

        bool boolValue = true;
        bool isBool = true;

        try
        {
            boolValue = (bool) value;
        }
        catch
        {
            isBool = false;
        }

        if (!isBool)
            return NullContent;

        return boolValue ? TrueContent : FalseContent;
    }

    public object ConvertBack(object value, Type targetType, 
           object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
} 

在您的XAML中,您需要实现该转换器。

<Window x:Class="Example.MainWindow"
    ...
    xmlns:l="clr-Example"
    ...>
    <Window.Resources>
        <l:BoolToContentConverter x:Key="converter" />
    </Window.Resources>
    ...
        <DataGridTextColumn Header="Code"
                Binding="{Binding Code}" />
            <DataGridTemplateColumn Header="Actions"
                                    Width="150">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"
                                    HorizontalAlignment="Center">
                            <Button Command="{Binding DataContext.AutomaticSellCommand,
                                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
                                    CommandParameter="{Binding}"
                                    Padding="10"
                                    Margin="0,2,2,2">
                                <iconPacks:PackIconModern Kind="CurrencyDollar" />
                                <Button.Style>
                                    <Style TargetType="{x:Type Button}"
                                           BasedOn="{StaticResource AccentedSquareButtonStyle}">
                                        <Setter Property="ToolTip"
                                                Value="DEFAULT_TOOLTIP" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsAutomaticSell, UpdateSourceTrigger=PropertyChanged}"
                                                         Value="True">
                                                <Setter Property="ToolTip"
                                                        Value="{Binding IsAutomaticSell, Converter={StaticResource converter}}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

    ....
</Window>

希望这能回答你的问题。干杯!