milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Playing with triggers

Details
Written by: Stanko Milosev
Category: WPF
Published: 08 April 2014
Last Updated: 06 April 2022
Hits: 5406

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.

Dispatcher processing has been suspended, but messages are still being processed.

Details
Written by: Stanko Milosev
Category: WPF
Published: 02 April 2014
Last Updated: 30 November -0001
Hits: 11489

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.

Update binding

Details
Written by: Stanko Milosev
Category: WPF
Published: 01 April 2014
Last Updated: 06 April 2022
Hits: 7012

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.

More about binding

Details
Written by: Stanko Milosev
Category: WPF
Published: 16 March 2014
Last Updated: 06 April 2022
Hits: 5659

I just want to show one more examples of binding.

Beside binding of TreeView to which I already gave an example here, there is another possibility of TreeView binding.

For this example I don't need model, so my view model looks like this:

public class ClassBinding
{
	public List<string> MyTest
	{
		get
		{
			List<string> pom = new List<string>();
			pom.Add("test");
			return pom;
		}
	}

	public ClassBinding MyContextTest
	{
	  get
	  {
		return new ClassBinding();
	  }
	}

}

My XAML for tree view binding looks like this:

<Grid>
	<TreeView DataContext="{Binding Path=MyContextTest}" ItemsSource="{Binding MyTest}"/>
</Grid>

Notice line {Binding Path=MyContextTest} then notice that MyContextClass is property of the class ClassBinding.

Example project you can download from here.

This article I wrote with help of my colleague from ISE.

  1. Autofac another example
  2. Resizable WPF controls
  3. Simple drag and drop
  4. Cursors

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 28 of 168

  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32