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

Open XAML without designer

Details
Written by: Stanko Milosev
Category: WPF
Published: 09 April 2014
Last Updated: 09 April 2014
Hits: 4419

Right click on the XAML file, and click "Open With..":

 

Choose Source Code (Text) Editor, and click "Set as Default":

Next time when you double click on XAML file, it will be opened without design, and faster.

 

Thanks to colleagues from ISE

Playing with triggers

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

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: 9977

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: 5558

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.

  1. More about binding
  2. Autofac another example
  3. Resizable WPF controls
  4. Simple drag and drop

Page 6 of 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10