使用样式更改按钮工具提示 - WPF [英] Change button tooltip with style - WPF
本文介绍了使用样式更改按钮工具提示 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。我有一个DataGrid加载了一个具有属性bool IsAutomaticSell的对象列表。我需要在更改值时,更新该行按钮
的工具提示。我有以下代码但它不起作用。 Thx
< 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}"
值=" True">
< Setter Property =" ToolTip"
Value =" NEW_TOOLTIP" />
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Button.Style>
< / Button>
< / StackPanel>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
public ICommand AutomaticSellCommand => _automaticSellCommand ??
(_automaticSellCommand = new RelayCommand< OrderStatusDataWrapper>(AutomaticSell));
private static void AutomaticSell(OrderStatusDataWrapper orderStatusData)
{
orderStatusData.IsAutomaticSell =!orderStatusData.IsAutomaticSell;
}
解决方案
嗨HajimeSaito ,
因为代码不完整,我无法在我身边重现这个问题,但我创建了一个简单的演示,如下所示,这很好。
#XAML
< Window x:Class =" ColeWPFSample.DataGridSample.Window2"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local =" clr-namespace:ColeWPFSample.DataGridSample"
mc:Ignorable =" d"
Title =" Window2"高度= QUOT; 450"宽度= QUOT; 800">
< Grid>
< StackPanel>
< DataGrid Name =" grid1" ItemsSource =" {Binding WindowModels}"的AutoGenerateColumns = QUOT假QUOT;>
< DataGrid.Columns>
< DataGridTextColumn Header =" Code" Binding =" {Binding Code}" />
< DataGridTemplateColumn Header =" Actions"宽度= QUOT; 150">
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< StackPanel Orientation =" Horizontal"的HorizontalAlignment = QUOT;中心">
< Button Command =" {Binding DataContext.AutomaticSellCommand,
RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type DataGrid}}}"
CommandParameter =" {Binding}"
Padding =" 10"
Margin =" 0,2,2,2">
< Button.Style>
< Style TargetType =" {x:Type Button}" >
< Setter Property =" ToolTip"值= QUOT; DEFAULT_TOOLTIP" />
< Style.Triggers>
< DataTrigger Binding =" {Binding IsAutomaticSell,UpdateSourceTrigger = PropertyChanged}"值= QUOT;真">
< Setter Property =" ToolTip"值= QUOT; NEW_TOOLTIP" />
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Button.Style>
< / Button>
< / StackPanel>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>
< / StackPanel>
< / Grid>
< / Window>
#Code Behind
使用System.Windows;
namespace ColeWPFSample.DataGridSample
{
///< summary>
/// Window2.xaml的互动逻辑
///< / summary>
public partial class Window2:Window
{
public Window2()
{
InitializeComponent();
this.DataContext = new Window2ViewModel();
}
}
}
#ViewModel
使用GalaSoft.MvvmLight.Command;
使用Prism.Mvvm;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Windows.Input;
namespace ColeWPFSample.DataGridSample
{
public class Window2ViewModel
{
private RelayCommand< OrderStatusDataWrapper> _automaticSellCommand;
public ObservableCollection< OrderStatusDataWrapper> WindowModels {get;组; }
public ICommand AutomaticSellCommand => _automaticSellCommand ??
(_automaticSellCommand = new RelayCommand< OrderStatusDataWrapper>(AutomaticSell));
private static void AutomaticSell(OrderStatusDataWrapper orderStatusData)
{
orderStatusData.IsAutomaticSell =!orderStatusData.IsAutomaticSell;
}
public Window2ViewModel()
{
List< OrderStatusDataWrapper> list = new List< OrderStatusDataWrapper>(){
new OrderStatusDataWrapper(){Id = 1,Code =" Test1",IsAutomaticSell = false},
new OrderStatusDataWrapper(){Id = 2,Code = " Test2",IsAutomaticSell = false},
new OrderStatusDataWrapper(){Id = 3,Code =" Test3",IsAutomaticSell = true},
new OrderStatusDataWrapper(){Id = 4,Code = " Test4",IsAutomaticSell = false},
new OrderStatusDataWrapper(){Id = 5,Code =" Test5",IsAutomaticSell = false}
};
WindowModels = new ObservableCollection< OrderStatusDataWrapper>(list);
}
}
公共类OrderStatusDataWrapper:BindableBase
{
private int _id;
private string _code;
private bool _isAutomaticSell;
public int Id
{
get
{
return _id;
}
设置
{
if(_id!= value)
{
_id = value;
RaisePropertyChanged(" Id");
}
}
}
公共字符串代码
{
get
{
return _code;
}
设置
{
if(_code!= value)
{
_code = value;
RaisePropertyChanged(" Id");
}
}
}
public bool IsAutomaticSell
{
get
{
return _isAutomaticSell;
}
设置
{
if(_isAutomaticSell!= value)
{
_isAutomaticSell = value;
RaisePropertyChanged(" IsAutomaticSell");
}
}
}
}
}
祝你好运,
张龙
p>
Hi. I have a DataGrid loaded with a list of objects that have a property bool IsAutomaticSell. I need that when changing the value, the tooltip of the button of that row is updated. I have the following code but it does not work. Thx
<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>
public ICommand AutomaticSellCommand => _automaticSellCommand ?? (_automaticSellCommand = new RelayCommand<OrderStatusDataWrapper>(AutomaticSell)); private static void AutomaticSell(OrderStatusDataWrapper orderStatusData) { orderStatusData.IsAutomaticSell = !orderStatusData.IsAutomaticSell; }
解决方案
Hi HajimeSaito,
Because the code is not complete, I could not reproduce the issue on my side, but I create a simple demo as below, which works fine.
#XAML
<Window x:Class="ColeWPFSample.DataGridSample.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ColeWPFSample.DataGridSample" mc:Ignorable="d" Title="Window2" Height="450" Width="800"> <Grid> <StackPanel> <DataGrid Name="grid1" ItemsSource="{Binding WindowModels}" AutoGenerateColumns="False"> <DataGrid.Columns> <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"> <Button.Style> <Style TargetType="{x:Type Button}" > <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> </DataGrid.Columns> </DataGrid> </StackPanel> </Grid> </Window>#Code Behind
using System.Windows; namespace ColeWPFSample.DataGridSample { /// <summary> /// Interaction logic for Window2.xaml /// </summary> public partial class Window2 : Window { public Window2() { InitializeComponent(); this.DataContext = new Window2ViewModel(); } } }#ViewModel
using GalaSoft.MvvmLight.Command; using Prism.Mvvm; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows.Input; namespace ColeWPFSample.DataGridSample { public class Window2ViewModel { private RelayCommand<OrderStatusDataWrapper> _automaticSellCommand; public ObservableCollection<OrderStatusDataWrapper> WindowModels { get; set; } public ICommand AutomaticSellCommand => _automaticSellCommand ?? (_automaticSellCommand = new RelayCommand<OrderStatusDataWrapper>(AutomaticSell)); private static void AutomaticSell(OrderStatusDataWrapper orderStatusData) { orderStatusData.IsAutomaticSell = !orderStatusData.IsAutomaticSell; } public Window2ViewModel() { List<OrderStatusDataWrapper> list = new List<OrderStatusDataWrapper>() { new OrderStatusDataWrapper(){ Id =1 , Code = "Test1", IsAutomaticSell = false}, new OrderStatusDataWrapper(){ Id =2 , Code = "Test2", IsAutomaticSell = false}, new OrderStatusDataWrapper(){ Id =3 , Code = "Test3", IsAutomaticSell = true}, new OrderStatusDataWrapper(){ Id =4 , Code = "Test4", IsAutomaticSell = false}, new OrderStatusDataWrapper(){ Id =5 , Code = "Test5", IsAutomaticSell = false} }; WindowModels = new ObservableCollection<OrderStatusDataWrapper>(list); } } public class OrderStatusDataWrapper : BindableBase { private int _id; private string _code; private bool _isAutomaticSell; public int Id { get { return _id; } set { if (_id != value) { _id = value; RaisePropertyChanged("Id"); } } } public string Code { get { return _code; } set { if (_code != value) { _code = value; RaisePropertyChanged("Id"); } } } public bool IsAutomaticSell { get { return _isAutomaticSell; } set { if (_isAutomaticSell != value) { _isAutomaticSell = value; RaisePropertyChanged("IsAutomaticSell"); } } } } }
Best regards,
Zhanglong
这篇关于使用样式更改按钮工具提示 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文