Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Sunday, August 3, 2014

MultiDataTrigger : Handle OR condition


Building on my previous post on MultiDataTrigger, we saw that MultiDataTrigger generally works for AND conditions (all conditions should be true to fire the trigger to update the property)

What would you do to handle OR condition?

<Style x:Key="pricestyle" TargetType="TextBox">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding AllowCheck}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                        <Setter Property="Visibility" Value="Visible"/>
                </MultiDataTrigger>
                <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Action}" Value="SELL"/>
                    </MultiDataTrigger.Conditions>
                        <Setter Property="Visibility" Value="Visible"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>

Effectively you would need to write 2 multi data triggers to get this working.

Side notes (not related to this post topic)
http://stackoverflow.com/questions/17598200/datatrigger-binding-in-wpf-style
http://stackoverflow.com/questions/15814639/consolidating-common-wpf-styles

Saturday, August 2, 2014

MultiDataTrigger in WPF: an example

Here is the XAML
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="pricestyle" TargetType="TextBox">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AllowCheck}" Value="True"/>
<Condition Binding="{Binding Action}" Value="SELL"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Visible"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel Margin="20" HorizontalAlignment="Left">
<Label Content="Action" Margin="0,3,0,0"></Label>
<ComboBox Width="100" ItemsSource="{Binding Actions}" SelectedItem="{Binding Action, UpdateSourceTrigger=PropertyChanged}"></ComboBox>
<Label Content="Price" Margin="0,3,0,0"></Label>
<TextBox Width="100" Margin="0,3,0,0" Style="{StaticResource ResourceKey=pricestyle}"></TextBox>
<CheckBox x:Name="dd" Content="Allow" IsChecked="{Binding AllowCheck, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox>
</StackPanel>
</Grid>
</Window>

On acceptable values defined in the trigger (highlighted in blue colour), on selection of Allow checkbox and SELL from action combo box, the trigger gets fired and the price textbox gets visible.


One important thing to note here (which may get missed out). Note the text in red colour. It's important to set a default state of the property for which you want the trigger to be applied on (in our example, its Visibility property of the textbox)

Sunday, July 6, 2014


XAML to ViewModel creator.

This demo is in works. Currently it reads a XAML file (using XElement) and identifies Bindings and their controls.

Next i'll complete this by printing out a view model class with properties using the bindings that i've collected in a collection.

You may want to improve this; feel free!

Download Demo Code

Placeholder trial: Placeholder code
Update to placeholder: Handle events of a control within a datatemplate. Refer this link:
http://stackoverflow.com/questions/1800595/event-handler-in-datatemplate

Tuesday, July 1, 2014


WPF: Quick way to display columnar data for printing. The code below displays textblocks in 2 columns. You can bind this listview or place data in XAML as shown below.

<ListView SelectionChanged="ListView_SelectionChanged_1" x:Name="lview"> <ListViewItem></ListViewItem> <ListView.View> <GridView x:Name="gview"> <GridViewColumn Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <TextBlock HorizontalAlignment="Stretch" Text="hello"/> <TextBlock HorizontalAlignment="Stretch" Text="hello"/> <TextBlock HorizontalAlignment="Stretch" Text="hello"/> <TextBlock HorizontalAlignment="Stretch" Text="hello"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <TextBlock HorizontalAlignment="Stretch" Text="mangesh"/> <TextBlock HorizontalAlignment="Stretch" Text="mangesh"/> <TextBlock HorizontalAlignment="Stretch" Text="mangesh"/> <TextBlock HorizontalAlignment="Stretch" Text="mangesh"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView>


Things such as hiding column headers, setting column width if required need to be done.

2 column grid example

Sunday, May 18, 2014

User controls for clutter free forms in WPF

User controls for clutter free forms in WPF

https://sites.google.com/site/mvnworldinc/Home/UserControlTest.zip?attredirects=0&d=1

Exposing inner Control properties for binding in WPF

http://stackoverflow.com/questions/4169090/exposing-inner-control-properties-for-binding-in-wpf

http://stackoverflow.com/questions/18158500/usercontrol-dependency-property-design-time

http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector

http://stackoverflow.com/questions/3922908/changing-contenttemplate-based-on-listbox-selection

http://breakingdotnet.blogspot.in/2012/05/data-template-selector-in-xaml.html

http://jacobaloysious.wordpress.com/2013/08/19/mvvm-using-contenttemplateselector-in-tab-control-view/

http://www.experts-exchange.com/Programming/Languages/.NET/Q_28029825.html

http://zamjad.wordpress.com/2011/09/21/using-contenttemplateselector/

Demo of how user controls can have their own VM
https://sites.google.com/site/mvnworldinc/Home/UsercontrolVM_Demo.zip?attredirects=0&d=1

Tab Header with context menu button:

Sample

Sample 2

Tuesday, April 15, 2014

Wiring Views and ViewModels using DataTemplate in resource dictionary in Prism:


Replace square brackets with <> tags

1. In module Initialize() :

// Merge Resource Dictionaries
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("pack://application:,,,/Project.AssemblyName;component/MainResourceDictionary.xaml ");
Application.Current.Resources.MergedDictionaries.Add(dictionary);

2. Create a main resource dictionary:

[ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vw="clr-namespace:Project.AssemblyName"
    xmlns:v="clr-namespace:Project.AssemblyName;assembly=Project.Views"]
    [!-- Resource dictionary entries should be defined here. --]
    [DataTemplate DataType="{x:Type vw:VM_1}"]
        [v:View_1 /]
    [/DataTemplate]
    [DataTemplate DataType="{x:Type vw:VM_2}"]
        [v:View_2 /]
    [/DataTemplate]
[/ResourceDictionary]

3. In Views project, your user control:

[UserControl x:Class="Project.Views.View_1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Resx="clr-namespace:Project.AssemblyName"
      ..

Question posted:

http://goo.gl/MI1YCz

Others:
http://wpfthemes.codeplex.com/discussions/43074
http://transitionals.codeplex.com/releases/view/15792
http://stackoverflow.com/questions/9796174/load-usercontrol-in-tabitem
https://compositewpf.codeplex.com/discussions/30673

Saturday, February 22, 2014

A tutorial application in WPF

When you're new to some project and new to its application, a tutorial could be of use to quickly understand the application, its purpose, what its components do and what is the intention of the application. While there is no substitute for personal training, a simple tutorial app could get you started quickly. Plus there is always an advantage to look back at it whenever you want without any time limitation.

Here is a simple idea and its simplest implementation in WPF. It's just a starter.

Download Demo

Update:
I thought it'd be easier, faster and better if this were implemented in html using jQuery. I got a chance to learn jQuery because of this and ended up making a demo page. Hope someone builds up on this.

Download Demo in JQuery

Update 28/03/14: Intro.js is an amazing jquery library. Thanks to the developers.

Demo with intro js

TL_App_Concept