- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 5123
First create dictionary as I explained here.
In my case I've created TriggersDictionary.xaml, so my main window XAML looks like this:
<Window x:Class="PlayingWithResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source= "TriggersDictionary.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Rectangle Style="{StaticResource styleWithTrigger}"></Rectangle>
</Grid>
</Window>
My style (TriggersDictionary.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="styleWithTrigger" TargetType="Rectangle">
<Setter Property="Fill" Value="LightGreen" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Here you can see property - this is event which will be executed as well as target type (in my case rectangle)
Example project you can download from here, and this article I wrote using this page.
----
2014-06-16 Update Properties which are supported for trigger, for rectangle, for example you can see here.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 11140
I was playing with TabControl, and I tried to implement SelectionChange event of TabControl.
So I did something like this:
XAML:
<Window x:Class="WpfApplication20.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Test1"></TabItem>
<TabItem Header="Test2"></TabItem>
</TabControl>
</Grid>
</Window>
Code behind:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("test");
}
With that code I was receiving error:
Dispatcher processing has been suspended, but messages are still being processed.
Solution:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(this.MyTest));
}
private void MyTest()
{
MessageBox.Show("test");
}
It seems that problem is in multi thread... Because for example code like this:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// ... Get TabControl reference.
var item = sender as TabControl;
// ... Set Title to selected tab header.
var selected = item.SelectedItem as TabItem;
this.Title = selected.Header.ToString();
}
Works without problem.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 6723
How to bind command to a button I explained here.
Here is my XAML:
<Window x:Class="UpdateBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:UpdateBinding.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModel:UpdateBindingViewModel x:Key="UpdateBindingBindingViewModel" />
</Window.Resources>
<Grid DataContext="{StaticResource UpdateBindingBindingViewModel}">
<StackPanel>
<TextBox Name="MyTextBox" Text="{Binding Ubm.Name}" VerticalAlignment="Top"/>
<Button VerticalAlignment="Top" Command="{Binding OnButtonClick}">test</Button>
</StackPanel>
</Grid>
</Window>
Command to update value looks like this:
public void UpdateBinding(object obj)
{
Ubm.Name = "Updated value";
OnPropertyChanged(() => this.Ubm);
}
Part of code:
protected void OnPropertyChanged<T>(Expression<Func<T>> property)
{
if (this.PropertyChanged != null)
{
var mex = property.Body as MemberExpression;
string name = mex.Member.Name;
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
I took from here.
Also don't forget to implement INotifyPropertyChanged interface, in my case declaration of viewmodel looks like this:
public class UpdateBindingViewModel: INotifyPropertyChanged
Example application you can download from here.
---
2014-06-11 Update Just a short notice. If you implement interface by default (with resharper alt + enter :) ) then to use it, you have to write which property of class (view model) are you updating. Something like:
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
if (propertyName == "test")
{
MyHidden = !MyHidden;
myContent = "Clicked!";
}
}
}
Where OnPropertyChanged is called like this:
OnPropertyChanged("MyHidden");
Obviously this example is not compatible with previous example :) I hope I will update this article soon...
---
2014-06-13 Another update
Check out following code:
namespace BooleanToVisibilityConverterExample.ViewModel
{
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using BooleanToVisibilityConverterExample.Annotations;
public class BooleanToVisibilityConverterExampleViewModel: INotifyPropertyChanged
{
public ICommand OnClick { get; set; }
public bool MyHidden { get; set; }
public BooleanToVisibilityConverterExampleViewModel()
{
OnClick = new RelayCommand(ShowHide);
MyHidden = true;
}
private void ShowHide(object obj)
{
MyHidden = !MyHidden;
OnPropertyChanged("MyHidden");
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Here notice lines:
MyHidden = !MyHidden;
OnPropertyChanged("MyHidden");
With these lines I said that I want to update property MyHidden which is bounded to rectangle visibility. This update actually is coming from this article, and example you can download from here.

