Archiv der Kategorie: WPF Windows Presentation Foundation

WPF DataGrid MouseBinding Gesture List

Problem

The MouseBinding Tag in XAML is declaring a property „Gesture“, but it is hard to find a full reference / a complete list for all gestures of the MouseBinding tag.

Solution

Accepted gestures can be found in the enumeration MouseAction

LeftClick1A left mouse button click.
LeftDoubleClick5A left mouse button double-click.
MiddleClick3A middle mouse button click.
MiddleDoubleClick7A middle mouse button double-click.
None0No action.
RightClick2A right mouse button click.
RightDoubleClick6A right mouse button double-click.
WheelClick4A mouse wheel rotation.
List of possible gestures

WPF MVVM: Was ist der Sinn hinter RelayCommand und wozu braucht man RelayCommand?

Problemfeld

RelayCommand (s) erfüllen den Zweck von Actions / Delegates und sind somit Funktionszeiger die es erlauben, zu einem späteren Zeitpunkt eine Methode aufzurufen.

Sie implementieren das Interface ICommand, welches von den WPF Controls genutzt wird, um bei Bedarf die Execute()-Methode aufzurufen. D.h. die Execute()-Methode soll nur dann aufgerufen werden, wenn das WPF Control das Ereignis tatsächlich auslöst und nicht bereits zur Bindung.

Ohne RelayCommands müsste man für jeden Command eine eigene Klasse schreiben, welche vom Interfache ICommand erbt. Sie helfen somit Code einzusparen.

Verwendung in einem MVVM Pattern

Häufige Verwendung: In einer XAML Datei wird ein Button definiert, der an einen RelayCommand gebunden wird, welcher zu einem späteren Zeitpunkt aufgerufen werden soll:

Das ViewModel, welches die View instanziert, muss einen RelayCommand „PerformCalibration“ definieren, der erst aufgerufen wird, wenn der Button geklickt wird (Relay) und nicht sofort bei der Bindung an den ButtonCommand.

Für diesen Zweck beinhaltet das ViewModel, welches die View instanziert, eine Funktion die den RelayCommand zurückgibt:
 

Der Button ruft die Funktion „Execute“ des implementierten RelayCommands auf, wenn er geklickt wird.

Da RelayCommands „Actions“ sind, haben Sie keinen Rückgabewert.

Visual Studio 2013 .NET WPF : Where do i get / can i find the WPF Ribbon Control?

Problem

According to the .NET documentation the Ribbon Control should be available since WPF 4.5, what means is available since .NET Framework 4.5 . But you can not find it? Are you even thinking about the download of the Ribbon control for older versions? Not necessary, please see the following points…

Approach

We have to find out the right moment when it is generally possible to use the Ribbon control.

The following table shows, that the first version of WPF (3.0) was released with the .NET Framework 3.0. As you can see the version number of WPF is maintained synchron with the version number of the .NET framework.

WPF VersionRelease (YYYY-MM).NET VersionVisual Studio VersionMajor Features
3.02006-113.0N/AInitial Release.
WPF development can be done with VS 2005 (released in Nov 2005) too with few additions as described here.
3.52007-113.5VS 2008Changes and improvements in:
Application model, data binding, controls, documents, annotations, and 3-D UI elements.
3.5 SP12008-083.5 SP1N/ANative splash screen support, New WebBrowser control, DirectX pixel shader support.
Faster startup time and improved performance for Bitmap effects.
4.02010-044.0VS 2010New controls: Calendar, DataGrid, and DatePicker.
Multi-Touch and Manipulation
4.52012-084.5VS 2012New Ribbon control
New INotifyDataErrorInfo interface
4.5.12013-104.5.1VS 2013No Major Change
4.5.22014-054.5.2N/ANo Major Change
4.62015-074.6VS 2015Transparent child window support
HDPI and Touch improvements
Which WPF Version is included in which version of the .NET Framework?

Solution

When you have installed at least .NET Framework 4.5 you can Add the Reference „System.Windows.Control.Ribbon“ as follows:

1.) Expand the tree in in your Solution Explorer of you WPF application

2.) Right-click the „Add References“ entry and choose „Add Reference“

In the Solution Explorer of your WPF Application right-click the References entry and choose „Add Reference“

3.) In the following dialog search for „Ribbon“ in the search field on the upper right

Search for Ribbon in the Dialog that appears by using the textbox on the upper right corner

4.) After that you should be able to add the reference by using the checkbox

5.) In your XAML Code you are now able to use the <Ribbon/> Tag

Optional: 6.) If you want to use a RibbonWindow instead of a WPF Window which allows you to have the Quick Access Controls at the top of the window, you have to do the declaration in the head of MainWindow.xaml as follows:

<ribbon:RibbonWindow x:Class="WPFTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon"
        Title="WPF Layoutmanager" Height="399" Width="763">
    <DockPanel LastChildFill="True">
        <Ribbon DockPanel.Dock="Top" >
        </Ribbon>
         <!-- Put the last Child here -->
    </DockPanel>
</ribbon:RibbonWindow>

Additionally you have to change the base class name in the code behind window and add the using System.Windows.Controls.Ribbon :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFTutorial
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Test");
        }
    }
}

.NET WPF : XAML locations to place and change WPF Styles

Intention

Like in HTML/CSS there are different source code locations where it is possible to apply appearance styles to WPF controls. In this article i want to make a really short summary of those points.

Approach

The following examples are showing the declaration places for styles in the order from local control styles to global application styles

Location examples

App.xaml (global application wide styles)

<Application x:Class="WPFTutorial.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="#ff0000" />
                <Setter Property="FontSize" Value="24" />
            </Style>
    </Application.Resources>
</Application>

MainWindow.xaml (local hierarchical inheritance from the WPF DOM)

<Window x:Class="WPFTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF Layoutmanager" Height="322" Width="528">
    <Window.Resources>
        <Style TargetType="TextBlock">
            <!-- This sets all TextBlock FontSize Styles within the Grid -->
            <Setter Property="FontSize" Value="15" />
            <Setter Property="Foreground" Value="red" />
        </Style>
        <Style TargetType="TabControl">
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
        </Style>
    </Window.Resources>
    <DockPanel>
        <TextBlock DockPanel.Dock="Top" VerticalAlignment="Center" Text="Ein Dockpanel.Dock=Top ist das hier und TabControl als LastChild gestretcht" />
        <TabControl>
            <TabItem Header="Grid">
                <Grid>
                    <Grid.Resources>
                        <Style TargetType="TextBlock">
                            <!-- This sets all TextBlock FontSize Styles within the Grid -->
                            <Setter Property="FontSize" Value="15" />
                        </Style>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <Button Width="140" Height="50" Click="Button_Click">
                        <StackPanel Orientation="Horizontal">
                            <Image Margin="10"  Source="Images/haken.png" Stretch="UniformToFill"/>
                            <TextBlock VerticalAlignment="Center" Text="Play Sound" />
                        </StackPanel>
                    </Button>
                    <Button Grid.Column="1" Grid.Row="2" Width="140" Height="50" Click="Button_Click">
                        <StackPanel Orientation="Horizontal">
                            <Image Margin="10"  Source="Images/haken.png" Stretch="UniformToFill"/>
                            <TextBlock VerticalAlignment="Center" Text="Play Sound" />
                        </StackPanel>
                    </Button>
                    <Button Grid.Column="2" Grid.Row="1" Width="140" Height="50" Click="Button_Click">
                        <StackPanel Orientation="Horizontal">
                            <Image Margin="10"  Source="Images/haken.png" Stretch="UniformToFill"/>
                            <TextBlock VerticalAlignment="Center" Text="Play Sound" >
                                <TextBlock.Style>
                                    <!-- Dieser Style gilt nur für den Textblock local und überschreibt den GRID Style -->
                                    <Style>
                                        <Setter Property="TextBlock.FontSize" Value="10" />
                                        <Setter Property="TextBlock.FontWeight" Value="Bold" />
                                        <Setter Property="TextBlock.Foreground" Value="#ff00ff" />
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </StackPanel>
                    </Button>
                </Grid>
            </TabItem>
            <TabItem Header="StackPanel">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="Dummfug ersetzt Irrsinn"/>
                    <TextBlock Text="Irrsinn ersetzt Dummfug" />
                    <TextBlock Text="Das letzte Child ist im Gegensatz zum Dockpanel nicht gestretcht" >
                        <TextBlock.Style>
                            <Style>
                                <Setter Property="TextBlock.Foreground" Value="#ff00ff" />
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </StackPanel>
            </TabItem>
        </TabControl>
    </DockPanel>
</Window>