我的ItemsControl有DataTemplate选择器,我希望实现以下目标:
<DataTemplate x:Key="buttonTemplate">
<Button if(someValue = true -> add thisPreviewMouseUp="button_MouseUp") PreviewMouseLeftButtonDown="button_MouseLeftButtonUp" PreviewMouseMove="button_MouseMove" Click="b_Click">
<Button.Content>
<Image Source="sample.png" Height="{Binding height}" Width="{Binding width}" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button.Content>
<Button.RenderTransform>
<RotateTransform Angle="{Binding angle}" />
</Button.RenderTransform>
</Button>
</DataTemplate>
用户可以移动,从经理模式更改按钮的大小,但我不想在正常模式下触发此事件(现在在mouse_move中有if(_fromWhere ==&#34; MANAGER&#34;))事件)
我知道如何让它发挥作用?
谢谢!
答案 0 :(得分:0)
我不知道,如果在XAML中可以设置要触发的事件。
但是,您可以在Tag property按钮中设置someValue
,然后在this link中建议的标记的触发事件检查中设置。
<Button Tag="{Binding someValue}" ...>
在事件处理程序中:
bool someValue = (bool)(((Button)sender).Tag);
修改1:
另一种方法是,如果someValue是按钮的DataContext的一部分,则获取按钮的DataContext:
var dataContext = ((Button)e.OriginalSource).DataContext;
if (dataContext.someValue)
{
...
}
编辑2:
经过一番调查后,我发现可以根据someValue
添加MouseMove事件。不幸的是,我认为这是不必要的复杂。
<Button Style="{Binding someValue, Converter={StaticResource BoolToButtonStyleConverter}, ConverterParameter={StaticResource MousePreviewEventSetStyle}, Mode=OneWay}" ...>
在Window.Resources中定义:
<l:BoolToButtonStyleConverter x:Key="BoolToButtonStyleConverter" />
<Style x:Key="MousePreviewEventSetStyle" TargetType="{x:Type Button}" >
<EventSetter Event="PreviewMouseUp" Handler="PreviewMouseUpClicked" />
</Style>
然后在Application.Resources中定义:
<Style x:Key="clearStyle" TargetType="{x:Type Button}" />
毕竟你必须定义:
public class BoolToButtonStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
bool someValue = (bool)value;
if (someValue)
return parameter;
else
return Application.Current.Resources["clearStyle"];
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最后设置事件的处理程序:
void PreviewMouseUpClicked(object sender, RoutedEventArgs e)
{
...
}
备忘录:可能有更好的方法来设置&#34;默认样式&#34;按钮定义&#34;清除风格&#34;。
答案 1 :(得分:0)
可能有帮助。
Mvvm方法
我认为这样做很简单,只有当条件成立时才启用事件。
我在这里做什么我在事件丢弃发生时执行命名 DropUser 的命令。我使用mvvmlight轻松指挥。您可以使用ICommand Wpf默认命令reference
xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvmlight="http://www.galasoft.ch/mvvmlight"
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Drop">
<mvvmlight:EventToCommand
Command="{Binding DataContext.DropUser,
PassEventArgsToCommand="True"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
视图模型
#region RelayCommand
private RelayCommand<DragEventArgs> _dropUser;
public RelayCommand<DragEventArgs> DropUser
{
get
{
return _dropUser ?? (_dropUser = new RelayCommand<DragEventArgs>(DropMethod,canExecute));
}
}
private bool canExecute(DragEventArgs arg)
{
// check your condition return true . Command is only work when you return true.
}
#endregion
// Method Will Fire here and do action here
private void DropMethod(DragEventArgs eventArgs)
{
if (eventArgs != null)
{
}
}
你需要的是这样的。
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="PreviewMouseLeftButtonDown">
<mvvmlight:EventToCommand
Command="{Binding CommandName,
PassEventArgsToCommand="True"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
视图模型
private RelayCommand<RoutedEventArgs> _commandName;
public RelayCommand<RoutedEventArgs>CommandName
{
get
{
return commandName ?? (commandName = new RelayCommand<RoutedEventArgs>(invokeAction, canExecuteMethod));
}
}
private bool canExecuteMethod()
{
check your condition return true
}
private void invokeAction(RoutedEventArgs event)
{
action you want to do
}