Trigger method in code behind from style - wpf

I would like to trigger the method SelectAllText() when the textbox background color is red. How can I bind to code behind.
xaml:
<TextBox Grid.Column="1" Grid.Row="0" Text="Text" MouseEnter="Test1MouseEnter" Background="{Binding TxtBoxcolor, Mode=OneWay}" Name="txbName">
<TextBox.Style>
<Style>
<Style.Triggers>
<Trigger Property="TextBox.Background" Value="Red">
<!--Trigger code behind-->
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
code behind:
public void SelectAllText()
{
txbName.SelectAll();
}

It's possible in your case to handle Changed event on the background in the code behind?
txbName.Background.Changed += Background_Changed;
and in the Background_Changed
private void Background_Changed(object sender, EventArgs e)
{
var brush = sender as Brush;
if(brush!=null)
{
if(brush == Brushes.Red)
{
txbName.SelectAll();
}
}
}

Here's a way to do this with an attached event. It can only handle raising change events for one property per control. To raise events on value changes for multiple properties, you'd need an attached property that's a collection of some object with a property name and an event, which would be more complicated to write. This really just demonstrates the concept, but it's sufficient for the specific problem you have in front of you right now.
public static class PropertyChange
{
public static readonly RoutedEvent PropertyChangeEvent =
EventManager.RegisterRoutedEvent("PropertyChangeEvent", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(PropertyChange));
public static void AddPropertyChangeEventHandler(DependencyObject d, RoutedEventHandler handler)
{
var uie = d as UIElement;
if (uie != null)
{
uie.AddHandler(PropertyChange.PropertyChangeEvent, handler);
}
}
public static void RemovePropertyChangeEventHandler(DependencyObject d, RoutedEventHandler handler)
{
var uie = d as UIElement;
if (uie != null)
{
uie.RemoveHandler(PropertyChange.PropertyChangeEvent, handler);
}
}
#region PropertyChange.PropertyName Attached Property
public static String GetPropertyName(UIElement obj)
{
return (String)obj.GetValue(PropertyNameProperty);
}
public static void SetPropertyName(UIElement obj, String value)
{
obj.SetValue(PropertyNameProperty, value);
}
public static readonly DependencyProperty PropertyNameProperty =
DependencyProperty.RegisterAttached("PropertyName", typeof(String), typeof(PropertyChange),
new PropertyMetadata(null, PropertyName_PropertyChanged));
private static void PropertyName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as UIElement;
var oldProperty = e.OldValue as String;
var newProperty = e.NewValue as String;
if (oldProperty != null)
{
var dpd = DependencyPropertyDescriptor.FromName(oldProperty, d.GetType(), d.GetType());
dpd.RemoveValueChanged(d, PropertyChangedHandler);
}
if (newProperty != null)
{
var dpd = DependencyPropertyDescriptor.FromName(newProperty, d.GetType(), d.GetType());
dpd.AddValueChanged(d, PropertyChangedHandler);
}
}
private static void PropertyChangedHandler(object component, EventArgs args)
{
var uie = component as UIElement;
uie.RaiseEvent(new RoutedEventArgs(PropertyChange.PropertyChangeEvent, uie));
}
#endregion PropertyChange.PropertyName Attached Property
}
Demo
XAML
<TextBox
Width="100"
x:Name="TestTextBox"
Text="Blah blah blah"
local:PropertyChange.PropertyName="Background"
local:PropertyChange.PropertyChangeEvent="TestTextBox_PropertyChangeEvent"
>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Code behind
private void TestTextBox_PropertyChangeEvent(object sender, RoutedEventArgs e)
{
var tb = (TextBox)sender;
var x = tb.Background as SolidColorBrush;
// Instead of examining the background color, I would much prefer to look directly
// at the validation: What happens if you decide to change the error background color
// to a darker shade of red? Or a GradientBrush? A cosmetic decision like that should
// not affect program behavior.
//
// But you didn't give any clues about how your validation is implemented, so that's
// your problem not mine.
if (x != null && x.Color == Colors.Red)
{
tb.Focus();
tb.SelectAll();
}
}

Related

WPF ToolBar - Detect when item is set into ToolBarOverflowPanel

I am aware of the IsOverflowOpen and HasOverflowItems properties but I am looking for a way to tell whether the item (button, radiobutton...) has moved into the ToolBarOverflowPanel so I can use a trigger to change its style.
I need this to be able to reproduce some UWP ToolBar styles (Windows 10 Mail, Word, Excel...). I have successfully reproduced most of the style and the only missing bit is to be able to changed the style of my item when it is in the overflow panel.
On the screenshot of what I am trying to reproduce, you can clearly see that the Special Ident and Line Spacing buttons have changed style based on whether they are displayed or overflowed.
You can't do it with xaml only. You have to either use the code behind or create some attached properties.
Here is the solution with the AttachedProperty:
First you need to create an helper class exposing 2 properties:
The IsInOverflowPanel read-only property that you will use to trigger the style change.
The TrackParentPanel property, which is the enable/disable mechanism.
Here is the implementation:
public static class ToolBarHelper
{
public static readonly DependencyPropertyKey IsInOverflowPanelKey =
DependencyProperty.RegisterAttachedReadOnly("IsInOverflowPanel", typeof(bool), typeof(ToolBarHelper), new PropertyMetadata(false));
public static readonly DependencyProperty IsInOverflowPanelProperty = IsInOverflowPanelKey.DependencyProperty;
[AttachedPropertyBrowsableForType(typeof(UIElement))]
public static bool GetIsInOverflowPanel(UIElement target)
{
return (bool)target.GetValue(IsInOverflowPanelProperty);
}
public static readonly DependencyProperty TrackParentPanelProperty =
DependencyProperty.RegisterAttached("TrackParentPanel", typeof(bool), typeof(ToolBarHelper),
new PropertyMetadata(false, OnTrackParentPanelPropertyChanged));
public static void SetTrackParentPanel(DependencyObject d, bool value)
{
d.SetValue(TrackParentPanelProperty, value);
}
public static bool GetTrackParentPanel(DependencyObject d)
{
return (bool)d.GetValue(TrackParentPanelProperty);
}
private static void OnTrackParentPanelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as UIElement;
if (element != null)
{
bool newValue = (bool)e.NewValue;
if (newValue)
{
element.LayoutUpdated += (s, arg) => OnControlLayoutUpdated(element);
}
}
}
private static void OnControlLayoutUpdated(UIElement element)
{
var isInOverflow = TreeHelper.FindParent<ToolBarOverflowPanel>(element) != null;
element.SetValue(IsInOverflowPanelKey, isInOverflow);
}
}
public static class TreeHelper
{
public static T FindParent<T>(this DependencyObject obj) where T : DependencyObject
{
return obj.GetAncestors().OfType<T>().FirstOrDefault();
}
public static IEnumerable<DependencyObject> GetAncestors(this DependencyObject element)
{
do
{
yield return element;
element = VisualTreeHelper.GetParent(element);
} while (element != null);
}
}
Then, for every items which need to change style do the following:
<Button x:Name="DeleteButton" Content="Delete" helpers:ToolBarHelper.TrackParentPanel="True">
<Button.Style>
<Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="helpers:ToolBarHelper.IsInOverflowPanel" Value="True">
<!-- The Overflow style setters -->
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

WPF TreeView not being populated

I am trying to dynamically populate a WPF tree by using a ViewModel, however, for some reason it's not working. Either the bindings either aren't properly or I am messing up somewhere in code behind.
Here's a sample of what I have.
In XAML I define my TreeView like so...
<TreeView DockPanel.Dock="Left" Width="200" DataContext="{Binding MessageTree}" ItemsSource="{Binding MessageTree}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="viewModel:Mail" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Subject}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
In Code Behing i have...
private Mail MessageTree { get; set; }
And
using (var mail = new MailParser())
{
int count = mail.GetMessageCount(DateTime.Today.AddDays(-10), DateTime.Today.AddDays(1));
MessageTree = new Mail();
for (int i = count - 1; i >= 0; i--)
{
MailMessage msg = mail.RetrieveMessage(i);
if (msg != null)
{
MessageTree.Add(msg);
}
if (backgroundWorker != null)
{
decimal perc = (100.0m - (((i + 1.0m)*100.0m)/count));
backgroundWorker.ReportProgress((int) perc, "Recebendo mensagens... " + perc.ToString("N2") + "%");
if (backgroundWorker.CancellationPending)
{
e.Cancel = true;
break;
}
}
}
}
Mail is defined as
public sealed class Mail : INotifyPropertyChanged
{
private readonly ObservableCollection<Mail> _children;
private readonly MailMessage _msg;
private readonly Mail _parent;
private bool _isExpanded;
private bool _isSelected;
public Mail()
{
_msg = new MailMessage {Subject = "Empty"};
_parent = null;
_children = new ObservableCollection<Mail>();
}
public Mail(MailMessage msg, Mail parent = null)
{
_msg = msg;
_parent = parent;
_children = new ObservableCollection<Mail>();
}
public IEnumerable<Mail> Children
{
get { return _children; }
}
public string Subject
{
get { return _msg.Subject; }
}
public bool IsExpanded
{
get { return _isExpanded; }
set
{
if (value != _isExpanded)
{
_isExpanded = value;
OnPropertyChanged();
}
if (_isExpanded && _parent != null)
_parent.IsExpanded = true;
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value != _isSelected)
{
_isSelected = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void Add(MailMessage msg)
{
_children.Add(new Mail(msg, this));
OnPropertyChanged("Children");
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
I can't find anything in it so different from examples found online that it wouldn't work. The Add method is incomplete, I still need some logic to decide whether to add them to the collection or to the collection of one of the collection members, but as is all my Mail objecys are beeing added to the collection but not showing up in the TreeView.
What totally obvious thing am i missing? Shouldn't the TreeView automaticly update as I add items to the collection?
What I want is for the TreeView to show The children of the MessageTree property, and those children's children.
EDIT: Couldn't see the whole thing on my phone - amended answer based on ability to actually see everything. :)
MOREEDIT: updated based on comments, let's start from scratch!
First off, if you're set on using the window/whatever as the datacontext, let's make it `INotifyPropertyChange...next, let's make "MessageTree" a collection of mails, not just a single one (it'll make binding semantics easier, trust me)
public class WhateverContainsTheTree : Window, INotifyPropertyChanged
{
public WhateverContainsTheTree()
{
this.Loaded += OnLoaded;
this._messageTree = new ObservableCollection<Mail>();
this.DataContext = this;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
_worker = new BackgroundWorker();
_worker.DoWork += WorkerWorkin;
_worker.RunWorkerAsync();
}
private BackgroundWorker _worker;
private ObservableCollection<Mail> _messageTree;
public ObservableCollection<Mail> MessageTree
{
get { return _messageTree; }
set { _messageTree = value; RaisePropertyChanged("MessageTree"); }
}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void WorkerWorkin(object sender, DoWorkEventArgs e)
{
// obviously, change this to your stuff; I added a ctor so I could pass a string
Thread.Sleep(3000);
Console.WriteLine("Ok, setting message tree");
Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
(Action)(() =>
{
var mail1 = new Mail("Mail#1:Changed from background thread");
var mail2 = new Mail("Mail#2:Submail of mail #1");
var mail3 = new Mail("Mail#3:Submail of mail #2");
var mail4 = new Mail("Mail#4:Submail of mail #1");
var mail5 = new Mail("Mail#5:Submail of mail #4");
mail1.Children.Add(mail2);
mail1.Children.Add(mail4);
mail2.Children.Add(mail3);
mail4.Children.Add(mail5);
MessageTree.Add(mail1);
})
);
}
}
Also, like I'd said in the original response, let's slightly tweak Mail.Children:
public ObservableCollection<Mail> Children
{
get { return _children; }
}
And here's what I used for the treeview xaml:
<TreeView DockPanel.Dock="Left" Width="200" ItemsSource="{{Binding MessageTree}}">
<TreeView.ItemContainerStyle>
<Style TargetType="{{x:Type TreeViewItem}}">
<Setter Property="IsExpanded" Value="{{Binding IsExpanded, Mode=TwoWay}}" />
<Setter Property="IsSelected" Value="{{Binding IsSelected, Mode=TwoWay}}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="viewModel:Mail" ItemsSource="{{Binding Children}}">
<TextBlock Text="{{Binding Subject}}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
If this STILL doesn't work, I'll just paste in the whole LINQPad blob I put together to test this.
Without seeing the entire setup, I'm not positive but my guess would be that since MessageTree is a plain CLR property (rather than something that raises PropertyChanged or a DependencyProperty or something, that the binding is occurring before your MessageTree = new Mail(); call. When you set it to a new instance, the binding system isn't getting notified since it is a plain property.
Another potential issue is that you say that code is in the code-behind. Just using that Binding syntax won't pick up a property from the code-behind. It's possible that you're setting that up somewhere else in the code that you didn't show us. But generally you aren't going to be binding from the View to the code-behind, you'd be binding to a ViewModel that was used as the DataContext for the view itself.
Had to give a name to the TreeView (Tree) and then after
MessageTree = new Mail();
insert
Tree.ItemSource = MessageTree.Children;
I find this ugly but at least it works now. Thank you all for trying to help.

Attached Properties in DataGrid

I am using an attached property to limit the input into textboxes and textblocks to either numeric or alphabetic. Now I would like to apply this attached property to a datagridtextcolumn.
I tried the following:
<DataGridTextColumn Header="Max" Width="50"
Binding="{Binding Path=Max, Mode=TwoWay"
Helper:InputService.NumericOnly="True">
and something like this:
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="Helper:InputService.NumericOnly" Value="True"/>
</Style>
</DataGridTextColumn.ElementStyle>
But it does not work.
How do I do it right?
My InputService contains the NumericOnly property:
public static readonly DependencyProperty NumericOnlyProperty = DependencyProperty.RegisterAttached(
"NumericOnly",
typeof(bool),
typeof(InputService),
new UIPropertyMetadata(false, OnNumericOnlyChanged));
public static bool GetNumericOnly(DependencyObject d)
{
return (bool)d.GetValue(NumericOnlyProperty);
}
public static void SetNumericOnly(DependencyObject d, bool value)
{
d.SetValue(NumericOnlyProperty, value);
}
private static void OnNumericOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
bool isNumericOnly = (bool)e.NewValue;
if (d is TextBox)
{
var textBox = (TextBox)d;
if (isNumericOnly)
{
textBox.PreviewTextInput += BlockNonDigitCharacters;
textBox.PreviewKeyDown += ReviewKeyDown;
}
else
{
textBox.PreviewTextInput -= BlockNonDigitCharacters;
textBox.PreviewKeyDown -= ReviewKeyDown;
}
}
else if (d is TextBlock)
{
var textBlock = (TextBlock)d;
if (isNumericOnly)
{
textBlock.PreviewTextInput += BlockNonDigitCharacters;
textBlock.PreviewKeyDown += ReviewKeyDown;
}
else
{
textBlock.PreviewTextInput -= BlockNonDigitCharacters;
textBlock.PreviewKeyDown -= ReviewKeyDown;
}
}
}
private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e)
{
foreach (char ch in e.Text)
{
if (Char.IsDigit(ch))
{
e.Handled = true;
}
}
}
Ok, this it what works for me:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Helper:InputService.NumericOnly" Value="True"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
Your property implementation expects only to be set on a TextBox or TextBlock. I would suggest that you put a breakpoint in your code and check what type of control it's actually being set on - I suspect you'll find it's the parent container of your cell, rather than the text control itself.
EDIT: based on your comment, you probably want to include the following in your binding:
Binding="{Binding Path=Max, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
This will cause the binding to refresh every time the property changes, where the default for most input controls is to fire when a control loses focus.

refactoring to an attached property

I am trying to change the code below from being an event setter to an attached property (just so I can clean up the code behind). I get an error in the setter saying the value cannot be null, but I don't see why yet.
Forgetting for a second whether this is a good idea or not, can someone help me get the attached property right?
Cheers,
Berryl
EventSetter (works but with code behind as shown)
<!-- SINGLE CLICK EDITING -->
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnPreviewMouseLeftButtonDown"/>
</Style>
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var cell = sender as DataGridCell;
cell.Activate();
}
Property setter (error)
<!-- SINGLE CLICK EDITING -->
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="begavior:DataGridCellProperties.SingleClickToEdit" Value="True"/>
</Style>
public class DataGridCellProperties
{
public static readonly DependencyProperty SingleClickToEditProperty =
DependencyProperty.RegisterAttached("SingleClickToEditProperty",
typeof(bool), typeof(DataGridCellProperties),
new PropertyMetadata(false, OnSingleClickToEditPropertyChanged));
[AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
[AttachedPropertyBrowsableForType(typeof(DataGridCell))]
public static bool GetSingleClickToEdit(DataGridCell obj) { return (bool)obj.GetValue(SingleClickToEditProperty); }
public static void SetSingleClickToEdit(DataGridCell obj, bool value) { obj.SetValue(SingleClickToEditProperty, value); }
private static void OnSingleClickToEditPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var sender = obj as UIElement;
if (sender == null) return;
if ((bool)e.NewValue)
{
sender.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown_EditCell;
}
else
{
sender.PreviewMouseLeftButtonDown -= OnPreviewMouseLeftButtonDown_EditCell;
}
}
private static void OnPreviewMouseLeftButtonDown_EditCell(object sender, MouseButtonEventArgs e)
{
var cell = sender as DataGridCell;
cell.Activate();
}
}
"SingleClickToEditProperty" in your d-prop registration should be "SingleClickToEdit".

Image Source In ControlTemplate WPF

I have a ControlTemplate which is for the Button control, in the ControlTemplate I have Image control which is used to displayed in the button, Now I want to set the Image Source at runt time as I have to copy paste the ControlTemplate for each of the button to set new image for new button.
Thanks in advance.
Maybe this is what you are looking for:
http://www.hardcodet.net/2009/01/create-wpf-image-button-through-attached-properties
http://blogs.msdn.com/knom/archive/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton.aspx
http://social.msdn.microsoft.com/Forums/en-US/vswpfdesigner/thread/8ba13699-7f7f-4ab6-8e3e-f7d787355d81
Hope this helps.
Regards,
Mihir Gokani
Generally speaking there are two ways you can set an image source at run time (the code samples below are in pseudo-code and would not compile):
1) In XAML using binding where the source of the binding will be some object's property containing the image source (this is the scenario Slugster was talking about):
This would be your object:
public class ViewModel
{
public string ImageURI {get;set;}
}
and will be presented by XAML where you have your button with image, and image source is set through binding:
<Image Source="{Binding Source=ViewModel; Path=ImageURI}"/>
2) By setting the image source from code-behind.
this will be your XAML where you have the button with image:
<Image x:Name="theImage"/>
and in the code-behind you set the source of that image:
theImage.Source = new BitmapImage(new Uri("yor image uri"));
You can access an item from inside the template by using the GetTemplateChild(string childName) method (with the name of your element as defined in the XAML), for example - If your image was defined like this:
<Image x:Name="MyImage" Stretch="Fill" />
then you would call this method like this:
Image myImage = GetTemplateChild("MyImage") as Image;
if (myImage != null)
{
myImage.Source = "/Images/MyPicture.jpg";
}
Note: You will not be able to use this method until AFTER OnApplyTemplate has been called for the control.
<Button x:Class="FunitureCtlLib.PressedImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="uc"><!--MinHeight="25" MinWidth="50"-->
<Button.Template>
<ControlTemplate>
<Grid>
<Image Name="imgDefault" Source="{Binding Path=DefaultImageSource,ElementName=uc}" Stretch="{Binding Path=ImageStretch,ElementName=uc}"></Image>
<ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Image.Source" TargetName="imgDefault" Value="{Binding Path=PressedImageSource,ElementName=uc}"></Setter>
<Setter Property="UIElement.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="UIElement.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Color="White" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
/// <summary>
/// ImageButton.xaml
/// </summary>
public partial class PressedImageButton : Button
{
#region dependency property
public static readonly DependencyProperty DefaultImageSourceProperty = DependencyProperty.Register("DefaultImageSource", typeof(ImageSource), typeof(PressedImageButton), new PropertyMetadata(null, new PropertyChangedCallback(DefaultImageSourceChangedCallback)));
public static readonly DependencyProperty PressedImageSourceProperty = DependencyProperty.Register("PressedImageSource", typeof(ImageSource), typeof(PressedImageButton), new PropertyMetadata(null, new PropertyChangedCallback(PressedImageSourceChangedCallback)));
public static readonly DependencyProperty ImageStretchProperty = DependencyProperty.Register("ImageStretch", typeof(Stretch), typeof(PressedImageButton), new PropertyMetadata(Stretch.None, new PropertyChangedCallback(ImageStretchChangedCallback)));
#endregion
#region callback
private static void DefaultImageSourceChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is PressedImageButton)
{
PressedImageButton imgbtn = sender as PressedImageButton;
imgbtn.OnDefaultImageSourceChanged(e.OldValue, e.NewValue);
}
}
private static void PressedImageSourceChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is PressedImageButton)
{
PressedImageButton imgbtn = sender as PressedImageButton;
imgbtn.OnPressedImageSourceChanged(e.OldValue, e.NewValue);
}
}
private static void ImageStretchChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is PressedImageButton)
{
PressedImageButton imgbtn = sender as PressedImageButton;
imgbtn.OnImageStretchChanged(e.OldValue, e.NewValue);
}
}
#endregion
#region public property
/// <summary>
///
/// </summary>
public ImageSource DefaultImageSource
{
get
{
return this.GetValue(DefaultImageSourceProperty) as ImageSource;
}
set
{
this.SetValue(DefaultImageSourceProperty, value);
}
}
/// <summary>
///
/// </summary>
public ImageSource PressedImageSource
{
get
{
return this.GetValue(PressedImageSourceProperty) as ImageSource;
}
set
{
this.SetValue(PressedImageSourceProperty, value);
}
}
/// <summary>
///
/// </summary>
public Stretch ImageStretch
{
get
{
return (Stretch)this.GetValue(ImageStretchProperty);
}
set
{
this.SetValue(ImageStretchProperty, value);
}
}
#endregion
#region protected method
protected void OnDefaultImageSourceChanged(object oldValue, object newValue)
{
//viewmodel.DefaultImageSource = newValue as ImageSource;
this.DefaultImageSource = newValue as ImageSource;
}
protected void OnPressedImageSourceChanged(object oldValue, object newValue)
{
//viewmodel.PressedImageSource = newValue as ImageSource;
this.PressedImageSource = newValue as ImageSource;
}
protected void OnImageStretchChanged(object oldValue, object newValue)
{
//viewmodel.ImageStretch = (Stretch)newValue;
this.ImageStretch = (Stretch)newValue;
}
#endregion
#region construct
public PressedImageButton()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(PressedImageButton_Loaded);
}
#endregion
#region private event
void PressedImageButton_Loaded(object sender, RoutedEventArgs e)
{
}
#endregion
}

Resources