An style for a tooltip only works partially - c#

I would like to have an standar tooltip for all the controls, setting the initial delay, the show delay and that it can be shown when is not enabled. In the control I only would need to set the text of the tooltip in each case.
I have this style:
<sys:Int32 x:Key="TooltipInitialShowDelay">2000</sys:Int32>
<sys:Int32 x:Key="TooltipInitialShowDelayLong">5000</sys:Int32>
<sys:Int32 x:Key="TooltipDisplayTime">60000</sys:Int32>
<Style TargetType="ToolTip" x:Key="ToolTipDefaultStyle">
<Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
<Setter Property="ToolTipService.InitialShowDelay" Value="{StaticResource TooltipInitialShowDelay}"/>
<Setter Property="ToolTipService.ShowDuration" Value="{StaticResource TooltipDisplayTime}"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding PlacementTarget.(dp:ToolTipDependencyProperty.Texto), RelativeSource={RelativeSource AncestorType=ToolTip}}" MaxWidth="400" TextWrapping='Wrap' />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
I have this dependency properties:
public static class ToolTipDependencyProperty
{
#region Texto
public static readonly DependencyProperty TextoProperty =
DependencyProperty.RegisterAttached(
"Texto",
typeof(string),
typeof(ToolTipDependencyProperty));
public static string GetTexto(DependencyObject obj)
{
return (string)obj.GetValue(TextoProperty);
}
public static void SetTexto(DependencyObject obj, string value)
{
obj.SetValue(TextoProperty, value);
}
#endregion Texto
#region InitialShowDelay
public static readonly DependencyProperty InitialShowDelayProperty =
DependencyProperty.RegisterAttached(
"InitialShowDelay",
typeof(int),
typeof(ToolTipDependencyProperty));
public static int GetInitialShowDelay(DependencyObject obj)
{
return (int)obj.GetValue(InitialShowDelayProperty);
}
public static void SetInitialShowDelay(DependencyObject obj, int value)
{
obj.SetValue(InitialShowDelayProperty, value);
}
#endregion InitialShowDelay
#region ShowDuration
public static readonly DependencyProperty ShowDurationProperty =
DependencyProperty.RegisterAttached(
"ShowDuration",
typeof(int),
typeof(ToolTipDependencyProperty));
public static int GetShowDuration(DependencyObject obj)
{
return (int)obj.GetValue(ShowDurationProperty);
}
public static void SetShowDuration(DependencyObject obj, int value)
{
obj.SetValue(ShowDurationProperty, value);
}
#endregion ShowDuration
}
And this is my button:
<Button Name="btnBuscar" Content="Buscar" Command="{Binding BuscarCommand}"
IsEnabled="{Binding BuscarIsEnabled}"
dp:ToolTipDependencyProperty.Texto="{Binding BuscarToolTip}">
<Button.ToolTip>
<ToolTip Style="{StaticResource ToolTipDefaultStyle}"/>
</Button.ToolTip>
</Button>
The problem is that the text is ahown, but only when the button is enabled. And also the initial show delay and show duration is the standard duration, not the values that I want to use.
However, if in the control I set the ToolTip.Service variables, it works.
How could I set the duration in the style?
Thanks.

The ToolTipService attached properties need to be set on the element that owns the tooltip, not the tooltip itself.
You'll need to create a style for Button, or FrameworkElement.

Related

Set Border.Brush property according to Mouse State and Position

I'm trying to change my UserControl's Border.BorderBrush property depending on if the Mouse enters, leaves, or is down on my UserControl. I've tried doing it explicitly in the code behind, but anytime the Border.BorderBrush property is changed, the border disappears.
I went down a string of possible solutions with no avail. As of my current codebase I'm attempting to use a Style and Triggers to manage it for me. My issue there is that there is no property for IsMouseDown unless you're dealing with a Button (at least that's what I've gathered from reading), so I defined a property for that.
Just when I think it's going to work, the border can't find my Style defined in UserControl.Resources.
I've exhausted everything I know to do, any help would be extremely appreciated.
XAML:
<UserControl x:Class="OSK.Resources.Themes.Default.Key"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OSK.Resources.Themes.Default"
mc:Ignorable="d"
x:Name="OSKuwuDefaultKey"
d:DesignHeight="48" d:DesignWidth="48">
<UserControl.Style>
<Style x:Name="KeyStyle">
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="true">
<Setter Property="Border.BorderBrush" Value="{Binding Path=BrushHover, RelativeSource={RelativeSource Self}}" />
</Trigger>
<Trigger Property="Border.IsMouseOver" Value="false">
<Setter Property="Border.BorderBrush" Value="{Binding Path=BrushNormal, RelativeSource={RelativeSource Self}}" />
</Trigger>
<Trigger Property="local:Key.IsMouseDown" Value="true">
<Setter Property="Border.BorderBrush" Value="{Binding Path=BrushDown, RelativeSource={RelativeSource Self}}" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<Border x:Name="key" Background="Transparent" Width="{Binding Path=Width, ElementName=OSKuwuDefaultKey}" Height="{Binding Path=Height, ElementName=OSKuwuDefaultKey}" BorderBrush="{Binding BorderBrush, ElementName=OSKuwuDefaultKey}" CornerRadius="{Binding Path=CornerRadius, ElementName=OSKuwuDefaultKey}" BorderThickness="{Binding Path=OutlineThickness, ElementName=OSKuwuDefaultKey}" MouseEnter="Key_MouseEnter" MouseLeave="Key_MouseLeave" MouseDown="Key_MouseDown" MouseUp="Key_MouseUp">
<Canvas>
<Label Content="{Binding SuperText, ElementName=OSKuwuDefaultKey}" Canvas.Top="6" Canvas.Left="8" />
<Label Content="{Binding SubText, ElementName=OSKuwuDefaultKey}" Canvas.Top="20" Canvas.Right="4" />
</Canvas>
</Border>
Code-Behind:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace OSK.Resources.Themes.Default
{
/// <summary>
/// Interaction logic for Key.xaml
/// </summary>
public partial class Key : UserControl
{
public static readonly DependencyProperty SuperTextProperty = DependencyProperty.Register("SuperText", typeof(string), typeof(Key), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
public string SuperText
{
get
{
return (string)GetValue(SuperTextProperty);
}
set
{
SetValue(SuperTextProperty, value);
}
}
public static readonly DependencyProperty SubTextProperty = DependencyProperty.Register("SubText", typeof(string), typeof(Key), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
public string SubText
{
get
{
return (string)GetValue(SubTextProperty);
}
set
{
SetValue(SubTextProperty, value);
}
}
public static readonly DependencyProperty BrushNormalProperty = DependencyProperty.Register("BrushNormal", typeof(Brush), typeof(Key), new FrameworkPropertyMetadata(Brushes.LightSlateGray));
public Brush BrushNormal
{
get
{
return (Brush)GetValue(BrushNormalProperty);
}
set
{
SetValue(BrushNormalProperty, value);
}
}
public static readonly DependencyProperty BrushHoverProperty = DependencyProperty.Register("BrushHover", typeof(Brush), typeof(Key), new FrameworkPropertyMetadata(Brushes.LightSteelBlue));
public Brush BrushHover
{
get
{
return (Brush)GetValue(BrushHoverProperty);
}
set
{
SetValue(BrushHoverProperty, value);
}
}
public static readonly DependencyProperty BrushDownProperty = DependencyProperty.Register("BrushDown", typeof(Brush), typeof(Key), new FrameworkPropertyMetadata(Brushes.SlateGray));
public Brush BrushDown
{
get
{
return (Brush)GetValue(BrushDownProperty);
}
set
{
SetValue(BrushDownProperty, value);
}
}
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(int), typeof(Key), new FrameworkPropertyMetadata(4, FrameworkPropertyMetadataOptions.AffectsRender));
public int CornerRadius
{
get
{
return (int)GetValue(CornerRadiusProperty);
}
set
{
SetValue(CornerRadiusProperty, value);
}
}
public static readonly DependencyProperty OutlineThicknessProperty = DependencyProperty.Register("OutlineThickness", typeof(Thickness), typeof(Key), new FrameworkPropertyMetadata(new Thickness(1), FrameworkPropertyMetadataOptions.AffectsRender));
public Thickness OutlineThickness
{
get
{
return (Thickness)GetValue(OutlineThicknessProperty);
}
set
{
SetValue(OutlineThicknessProperty, value);
}
}
public static DependencyProperty IsMouseDownProperty = DependencyProperty.RegisterAttached("IsMouseDown", typeof(bool), typeof(Key), new FrameworkPropertyMetadata(default(bool)));
public bool IsMouseDown
{
get
{
return (bool)GetValue(IsMouseDownProperty);
}
set
{
SetValue(IsMouseDownProperty, value);
}
}
public Key()
{
InitializeComponent();
}
private void Key_MouseEnter(object sender, MouseEventArgs e)
{
//this.SetValue(Key.BorderBrushProperty, BrushNormal);
//Console.WriteLine(GetValue(Key.BorderBrushProperty));
}
private void Key_MouseLeave(object sender, MouseEventArgs e)
{
//this.SetValue(Key.BorderBrushProperty, BrushHover);
}
private void Key_MouseDown(object sender, MouseButtonEventArgs e)
{
//this.SetValue(Key.BorderBrushProperty, BrushDown);
this.SetValue(Key.IsMouseDownProperty, true);
}
private void Key_MouseUp(object sender, MouseButtonEventArgs e)
{
this.SetValue(Key.IsMouseDownProperty, false);
}
}
}
I got it working. The original post has the final working code.
And the layout should look like this, without explicitly setting the size of the Border:
<Border x:Name="key" Background="Transparent"
BorderBrush="{Binding BorderBrush, ElementName=OSKuwuDefaultKey}"
CornerRadius="{Binding CornerRadius, ElementName=OSKuwuDefaultKey}"
BorderThickness="{Binding OutlineThickness, ElementName=OSKuwuDefaultKey}"
MouseEnter="Key_MouseEnter"
MouseLeave="Key_MouseLeave"
MouseDown="Key_MouseDown"
MouseUp="Key_MouseUp">
<Canvas>
...
</Canvas>
</Border>

wpf MVVM Focus cursor on textbox

In a WPF window, I'm trying to put the cursor by default on one of the textboxes.
After reading some questions and answers, I tried the following:
xaml:
<StackPanel Grid.Row="1"
<StackPanel.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding UserShouldEditValueNow}" Value="true">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=FID}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBox Name ="FID" Text="{Binding FixID, UpdateSourceTrigger=PropertyChanged}"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
</StackPanel>
cs: (Viewmodel)
this.UserShouldEditValueNow = true;
I expected to see a blinking cursor on the textbox FID when opening the window.
However, there's no cursor at all on this textbox.
Debugging showed me that I'm going through the cs code, setting the value to true.
any ideas why?
Solution involves:
1. Adding a FocusExtension class.
2. Focus and Keyboard.Focus are inside Dispatcher.BeginInvoke
cs.
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof(bool), typeof(FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.Dispatcher.BeginInvoke(
new Action(
delegate{
uie.Focus();
Keyboard.Focus(uie);
}
)
);
}
}
}
.xaml
<TextBox Text="{Binding FixID, UpdateSourceTrigger=PropertyChanged}" viewModels:FocusExtension.IsFocused="{Binding UserShouldEditValueNow}" />

Get Treeviewitem from Menu Context. WPF XAML MVVM

Good day.
I have created a TreeViewHelper Class in order to trigger events and pass through datacontext to functions. However. Id like to select the treeviewitem when right clicking.
My solution follows the MVVM pattern as close as possible. So if possible i'd like to avoid the Trigger Event feature that is provided.
Id love for some advice on how to tackle this issue.
here is the treeviewhelper.cs class
public static class TreeViewHelper
{
#region SelectedItem
public static object GetSelectedItem(DependencyObject obj)
{
return (object)obj.GetValue(SelectedItemProperty);
}
public static void SetSelectedItem(DependencyObject obj, object value)
{
obj.SetValue(SelectedItemProperty, value);
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.RegisterAttached("SelectedItem", typeof(object), typeof(TreeViewHelper), new UIPropertyMetadata(null, SelectedItemChanged));
private static void SelectedItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs dpea)
{
if (!(obj is TreeView) || dpea.NewValue == null)
return;
var view = obj as TreeView;
view.SelectedItemChanged += (sender, e) => SetSelectedItem(view, e.NewValue);
var command = (ICommand)(view as DependencyObject).GetValue(SelectedItemChangedProperty);
if (command != null)
{
if (command.CanExecute(null))
command.Execute(new DependencyPropertyEventArgs(dpea));
}
}
#endregion
#region Selected Item Changed
public static ICommand GetSelectedItemChanged(DependencyObject obj)
{
return (ICommand)obj.GetValue(SelectedItemProperty);
}
public static void SetSelectedItemChanged(DependencyObject obj, ICommand value)
{
obj.SetValue(SelectedItemProperty, value);
}
public static readonly DependencyProperty SelectedItemChangedProperty =
DependencyProperty.RegisterAttached("SelectedItemChanged", typeof(ICommand), typeof(TreeViewHelper));
#endregion
#region Event Args
public class DependencyPropertyEventArgs : EventArgs
{
public DependencyPropertyChangedEventArgs DependencyPropertyChangedEventArgs { get; private set; }
public DependencyPropertyEventArgs(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
this.DependencyPropertyChangedEventArgs = dependencyPropertyChangedEventArgs;
}
}
#endregion
}
Here is the XAML Code (shortened)
<TreeView x:Name="TestPlanTreeView"
ItemsSource="{Binding Data.TreeViewCollection}"
utils:TreeViewHelper.SelectedItem="{Binding CurrSelItem}"
utils:TreeViewHelper.SelectedItemChanged="{Binding SelItemChgCmd}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu ItemsSource="{Binding MenuCollection, RelativeSource={RelativeSource Self}}">
<ContextMenu.Resources>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Command}" />
</Style>
</ContextMenu.Resources>
<ContextMenu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Header}"/>
</HierarchicalDataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<TextBlock Text="{Binding Header}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
This is how i trigger a function when selecting a new item.
SelItemChgCmd = new RelayCommand<DependencyPropertyEventArgs>(Data.TreeViewItemSelected);
CurrSelItem = new object();
public void TreeViewItemSelected(DependencyPropertyEventArgs dpe)
{
TreeViewItem selectedTreeViewItem = dpe.DependencyPropertyChangedEventArgs.NewValue as TreeViewItem;
ListViewCollection = BuildListViewCollection(selectedTreeViewItem);
}
What I would love to do, is to be able to select the treeviewitem before right clicking on the object. However, I am not sure how to do that with my given code without hacking it.
My original thoughts were to create a property in the treeviewhelper for PreviewRightMouseButtonUp and have that trigger the SelectedItem event, but i then run into issues with data.
Since the PreviewRightMouseButtonUp does not hold the same method types as the selectedItem Changed event, then i run into these data issues.
Id really appreciate some thoughts on this matter. Its been blocking me for a while now.

Update Normal Property in Dependency property/AttachedProperty,

I am trying to bind a normal property of AvalonDock,
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
<xcad:LayoutAnchorable Title="Folder" CanHide="{Binding IsHideExplorerView}">
<Views:ExplorerView DataContext="{Binding ExplorerViewModel}"/>
</xcad:LayoutAnchorable>
Here CanHide is a Normal property, if trying to bind will throw the exception like
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
My question is, Is it possible any way to make a normal property to override DependencyProperty to make it Bindable.
Edit
Added a class which inherit LayoutAnchorable but PropertyChangedCallback of DependencyProperty Never calls.
public class ExtendedAnchorableItem : LayoutAnchorable
{
public static readonly DependencyProperty IsCanHideProperty =
DependencyProperty.RegisterAttached("IsCanHide", typeof(bool), typeof(ExtendedAnchorableItem),
new FrameworkPropertyMetadata((bool)false,
new PropertyChangedCallback(OnCanHideChanged)));
public bool IsCanHide
{
get { return (bool)GetValue(IsCanHideProperty); }
set { SetValue(IsCanHideProperty, value);
this.IsVisible = value; // No effect.
}
}
private static void OnCanHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ExtendedAnchorableItem)d).Hide();
}
}
XAML
<xcad:LayoutAnchorablePane>
<Utility:ExtendedAnchorableItem IsCanHide="{Binding IsHideExplorer}">
<Views:ExplorerView DataContext="{Binding ExplorerViewModel}"/>
</Utility:ExtendedAnchorableItem>
</xcad:LayoutAnchorablePane>
Similarly i have tried creating an AttachedProperty which can hook it to LayoutAnchorable but PropertyChangedCallback Never get called click here for a new question i have posted.
Any Help guys ?
I did and example previously in my case i need to create new button with 2 images one when the button is available and the other one when it's disabled, to do that first i created new user control named "MyButton" my xaml was like this
<Button ToolTip="{Binding ButtonLabel,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"
Command="{Binding ButtonCommand,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"
Cursor="Hand" VerticalAlignment="Center" >
<Button.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="45"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Name="ButtonImage" IsEnabled="{Binding Path=IsEnabled,RelativeSource={RelativeSource AncestorType=Button,Mode=FindAncestor}}" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Source" Value="{Binding ActiveImage,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Source" Value="{Binding DeactiveImage,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Label Name="LabelContent" Content="{Binding ButtonLabel,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},UpdateSourceTrigger=PropertyChanged}"
Grid.Column="1" IsEnabled="{Binding Path=IsEnabled,RelativeSource={RelativeSource AncestorType=Button,Mode=FindAncestor}}" VerticalContentAlignment="Center" />
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
then i added dependency Properties for ActiveImage and DeactiveImage using this code
public static DependencyProperty activeImage =
DependencyProperty.Register("ActiveImage", typeof(type of this property like "string"), typeof(type of the custom control that you need like "MyButton"), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string ActiveImage
{
get { return (string)GetValue(activeImage); }
set { SetValue(activeImage, value); }
}
then i used this new control in my project
<custom:MyButton ButtonCommand="{Binding DecreaseImagesCount}" ButtonLabel="ZoomIn" ActiveImage="/Images/ActiveImages/ZoomIn.png" DeactiveImage="/Images/GrayImages/ZoomIn.png"
Grid.Column="2" Margin="3,4" />
notice that i can do binding the path for Button Image now
If it is enough for you to just set that property from your view model then you could use an attached behavior.
Just create a new class and add an attached property like this (I did not really test this, since I actually do not have AvalonDock at hand, but you should get the idea):
public class YourBehavior
{
public static readonly DependencyProperty YourCanHideProperty = DependencyProperty.RegisterAttached(
"YourCanHide",
typeof(bool),
typeof(LayoutAnchorable),
new PropertyMetadata(YourCanHidePropertyChanged));
private static void YourCanHidePropertyChanged(
DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
LayoutAnchorable control = dependencyObject as LayoutAnchorable;
if (control != null)
{
control.CanHide = e.NewValue as bool;
}
}
public static bool GetYourCanHideProperty(LayoutAnchorablewindow)
{
return window.GetValue(YourProperty) as bool?;
}
public static void SetYourCanHideProperty(LayoutAnchorable control, bool value)
{
window.SetValue(YourProperty, value);
}
}
Now you should be able to use that behavior like this:
<xcad:LayoutAnchorable Title="Folder" namespacealias:YourBehavior.YourCanHideProperty="{Binding IsHideExplorerView}"/>
If you want to have it working in both directions just check out the attached Blend behaviors.
Yes, you can do it.. you need to implement INotifypropertyChanged interface and raise a ProprtyChanged Event inside the property setter. After changing the property to a DependencyProperty, you will get the notification mechanism, so the property change is propagated to the target (in this case xcad) .
you can find lot of examples implementing the INotifyPropertyChanged..

How to set WPF behavior property using a data trigger

I am trying to set a WPF behavior property using a style in the following way:
<StackPanel>
<CheckBox Name="IsFemaleChkBox" Content="Is Female ?" />
<TextBlock>
<Hyperlink> <!--setting property directly like this: local:MyHyperLinkBehavior.Salutation="Mr." isn't working either-->
<TextBlock Text="My Hyperlink"/>
<Hyperlink.Style>
<Style TargetType="Hyperlink">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Mr." />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=IsFemaleChkBox}" Value="True">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Miss" />
</DataTrigger>
</Style.Triggers>
</Style>
</Hyperlink.Style>
</Hyperlink>
</TextBlock>
</StackPanel>
And the behavior class code is this:
class MyHyperLinkBehavior : Behavior<Hyperlink>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += AssociatedObject_Click;
}
public static bool GetIsFemale(DependencyObject obj)
{
return (bool)obj.GetValue(IsFemaleProperty);
}
public static void SetIsFemale(DependencyObject obj, bool value)
{
obj.SetValue(IsFemaleProperty, value);
}
// Using a DependencyProperty as the backing store for IsFemale. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsFemaleProperty =
DependencyProperty.RegisterAttached("IsFemale", typeof(bool), typeof(MyHyperLinkBehavior), new PropertyMetadata(false));
public static string GetSalutation(DependencyObject obj)
{
return (string)obj.GetValue(SalutationProperty);
}
public static void SetSalutation(DependencyObject obj, string value)
{
obj.SetValue(SalutationProperty, value);
}
// Using a DependencyProperty as the backing store for Salutation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SalutationProperty =
DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(default(string)));
void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show(Convert.ToString(GetValue(SalutationProperty)));
}
}
I can't figure out why this is not working. Or setting a behavior's property using style isn't valid at all ? What is the other way round, if this is not valid.
There are two types of behaviors in WPF:
System.Windows.Interactivity Behaviors, called also Blend Behaviours
These behaviours are classes inherited from System.Windows.Interactivity.Behavior and you can use them by adding to used them by Adding it to Behaviours collection, e.g:
<Rectangle>
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior />
</i:Interaction.Behaviors>
</Rectangle>
notice, that these behaviors does not have any custom attached properties. OnAttached and OnDetached methods are automatically called.
Pros: Easy to implement
Cons: Does not work with styles (however, it works with ControlTemplates and DataTemplates)
Behaviors implemented as Custom Attached Property
In these behaviors the logic defined in PropertyChangedCallback of the custom attached property.
public static readonly DependencyProperty SalutationProperty =
DependencyProperty.RegisterAttached("Salutation",
typeof(string),
typeof(MyHyperLinkBehavior),
new PropertyMetadata(OnSalutationPropertyChanged));
private static void OnSalutationPropertyChanged(object sender,
DependencyPropertyChangedEventArgs e)
{
//attach to event handlers (Click, Loaded, etc...)
}
Pros: Can be defined in styles, easier to use
Cons: Chatty code, a little more difficult to implement
You are mixing those two types of behaviors together. Choose one and use it! Since you want to use it in style, you shoud choose behavior implemented as custom attached property
I got it working, it was a small miss by me.
I forgot to set the behavior on the hyperlink.
I need to get the property of the attachedObject and not of the
behavior.
Following code works fine:
<StackPanel>
<CheckBox Name="IsFemaleChkBox" Content="Is Female ?" />
<TextBlock>
<Hyperlink>
<TextBlock Text="My Hyperlink"/>
<i:Interaction.Behaviors> <!--Missed setting behavior-->
<local:MyHyperLinkBehavior />
</i:Interaction.Behaviors>
<Hyperlink.Style>
<Style TargetType="Hyperlink">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Mr." />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=IsFemaleChkBox}" Value="True">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Miss" />
</DataTrigger>
</Style.Triggers>
</Style>
</Hyperlink.Style>
</Hyperlink>
</TextBlock>
</StackPanel>
And the behavior:
class MyHyperLinkBehavior : Behavior<Hyperlink>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += AssociatedObject_Click;
}
public static bool GetIsFemale(DependencyObject obj)
{
return (bool)obj.GetValue(IsFemaleProperty);
}
public static void SetIsFemale(DependencyObject obj, bool value)
{
obj.SetValue(IsFemaleProperty, value);
}
// Using a DependencyProperty as the backing store for IsFemale. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsFemaleProperty =
DependencyProperty.RegisterAttached("IsFemale", typeof(bool), typeof(MyHyperLinkBehavior), new PropertyMetadata(false));
public static string GetSalutation(DependencyObject obj)
{
return (string)obj.GetValue(SalutationProperty);
}
public static void SetSalutation(DependencyObject obj, string value)
{
obj.SetValue(SalutationProperty, value);
}
// Using a DependencyProperty as the backing store for Salutation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SalutationProperty =
DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(default(string)));
void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
// Changing "GetValue(SalutationProperty)" to "this.AssociatedObject.GetValue(SalutationProperty)" works
MessageBox.Show(Convert.ToString(this.AssociatedObject.GetValue(SalutationProperty)));
}
}

Categories