attachedbehaviors

WPF Behavior AssociatedObject is null

℡╲_俬逩灬. 提交于 2020-07-11 03:16:49
问题 I'm setting a behavior on my control (in xaml), the behavior has the following code: protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += OnMycontrolLoaded; AssociatedObject.Unloaded += OnMycontrolUnloaded; } private void OnMycontrolLoaded(object sender, RoutedEventArgs e) { AssociatedObject.MyEvent +=MyEventHandler; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.Loaded -= OnMycontrolLoaded; AssociatedObject.Unloaded -=

Event unsubscription via anonymous delegate [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-01-01 01:35:29
问题 This question already has answers here : Unsubscribe anonymous method in C# (11 answers) Closed 3 years ago . I am using Resharper 5.1 code analysis many a times i get a comment from resharper as "Event unsubscription via anonymous delegate" #Part of Code if (((bool)e.NewValue)) { listView.PreviewTextInput += (o,args) => listView_PreviewTextInput(o,args,listView); } else { listView.PreviewTextInput -= (o, args) => listView_PreviewTextInput(o, args, listView); } How could i correct or optimze

Using MouseDragElementBehavior with an ItemsControl and Canvas

对着背影说爱祢 提交于 2019-12-31 02:52:06
问题 I am currently having a problem using the MouseDragElementsBehavior from the Blend SDK when using a ItemsControl and a Custom Canvas. My custom canvas simply adds or removes the MouseDragElement from its children depending on a DependencyProperty. This worked just fine when I was manually adding Items to the Canvas' children but appears to have broken when moving to an ItemsControl. I am currently using the following ItemsControl code: <ItemsControl ItemsSource="{Binding Path=CanvasItems}">

Silverlight: VisualStateManager.GetVisualStateGroups doesn't, How can I get them?

假如想象 提交于 2019-12-24 01:51:34
问题 I've tried using VisualStateManager.GetVisualStateGroups in the OnAttached override of my custom behavior, as well as in an event handler added to AssociatedObject.Loaded event in that behavior. Both times I get an empty list. Is there another way to get the visual state groups defined for a control, or another event handler I should attach to? Be for it's asked, yes, the control has VisualStateGroups and VisualStates . 回答1: Usually the VisualStateGroups attached property is attached to the

Attach XAML Behavior to all controls of same type

久未见 提交于 2019-12-24 00:14:55
问题 I have an InvokeCommandAction that I have that is attached to the GotFocus event of a TextBox like so: <TextBox Grid.Row="0" Grid.Column="1" Width="40" HorizontalAlignment="Right"> <i:Interaction.Triggers> <i:EventTrigger EventName="GotFocus"> <i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="Enter data [message to be displayed]" /> </i:EventTrigger> </i:Interaction.Triggers> </TextBox> It works just fine this way, but I have dozens of TextBox es with this same

MVVM handling the Drag events of MouseDragElementBehavior

試著忘記壹切 提交于 2019-12-22 12:54:27
问题 This question tells me what to do in words, but I can't figure out how to write the code. :) I want to do this: <SomeUIElement> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior ConstrainToParentBounds="True"> <i:Interaction.Triggers> <i:EventTrigger EventName="DragFinished"> <i:InvokeCommandAction Command="{Binding SomeCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> </ei:MouseDragElementBehavior> </i:Interaction.Behaviors> </SomeUIElement> But as the other question outlines, the

Drag Drop Row behavior on WPF DataGrid

有些话、适合烂在心里 提交于 2019-12-21 20:45:22
问题 I am trying to make an attached behavior to reorder rows bby doing Drag & Drop I found some solution (On Stackoverflow and by googling) and using them i am trying to make the behavior... I took the example from Hordcodenet website (i dont have the link now) Code public static class DragDropRowBehavior { private static DataGrid dataGrid; private static Popup popup; private static bool enable; private static object draggedItem; public static object DraggedItem { get { return DragDropRowBehavior

Attach behaviour to all TextBoxes in Silverlight

时间秒杀一切 提交于 2019-12-21 04:54:15
问题 Is it possible to attach behavior to all TextBoxes in Silverlight application? I need to add simple functionality to all text boxes. (select all text on focus event) void Target_GotFocus(object sender, System.Windows.RoutedEventArgs e) { Target.SelectAll(); } 回答1: You can override the default style for TextBoxes in your app. Then in this style, you can use some approach to apply a behavior with a setter (generally using attached properties). It would something like this: <Application

Reducing boilerplate code in MVVM WPF app for attached properties, commands, etc?

半腔热情 提交于 2019-12-20 20:32:56
问题 I'm working on a WPF MVVM application. The thing that I'm noticing is that I have to write an inordinate amount of boilerplate code just to declare commands (through DelegateCommands from the WPF Team's MVVM Toolkit), attached properties and attached behaviors. Are there any strategies available to reduce the amount of boilerplate code I have to write? Thanks! 回答1: Personally, I like Josh Smith's MVVM Foundation library. He uses a class called RelayCommand there, but it sounds pretty similar

SL4: need to register for a move (or redraw) event on an Item in an ItemsControl

老子叫甜甜 提交于 2019-12-13 03:35:46
问题 Not finding a move event or redraw event in the FrameworkElement class. And Google not helping either. So... I have a custom ItemsControl populated by an observable collection in the VM. The ItemsControl itself leverages the <i:Interaction.Behaviors> <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/> </i:Interaction.Behaviors> behavior so the user can drag around the whole assembly. When the user moves the assembly, I want to be notified by each item as the item is repositioned as