How do I combine PreviewMouseLeftButtonDown with Command? - wpf

In sample project I have a button binded to a Command. It works as button-click event.
However, I would like to use PreviewMouseLeftButtonDown/Up event in in conjunction with Command.
For example LeftButtonDown - filling with green, LeftButtonUp -default filling.
Is there a neat way to do that?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button Command="{Binding ColourChange_command, Mode=OneWay}" Content="Fill with green" />
</StackPanel>
<Ellipse Grid.Column="1" Stroke="Black">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Triggers>
<DataTrigger Binding="{Binding ColourChange}" Value="True">
<Setter Property="Fill" Value="LimeGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
</Grid>

Related

WPF Resource Styles not showing first time

When I bind my collection to the following window and usercontrol, the styles do not work.
When I press a button on the window, the styles kick in.
What is stopping my styles from firing at initial bind?
<Grid>
<ItemsControl Name="LbItems" ItemsSource="{Binding MyData}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type viewModel:SomeViewModel}">
<control:SomeView Margin="5" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:AnotherViewModel}">
<control:AnotherView Margin="5" />
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Grid>
And I have a user control as follows:
<UserControl.Resources>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={x:Static
RelativeSource.Self}, Path=DataContext.Selected}" Value="False">
<Setter Property="Foreground">
<Setter.Value>
Red
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={x:Static
RelativeSource.Self}, Path=DataContext.Selected}" Value="True">
<Setter Property="Foreground">
<Setter.Value>
DarkSeaGreen
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Command="{Binding SelectCommand}" Content="+" HorizontalAlignment=
"Left" VerticalAlignment="Top" Width="25" Grid.Column="0"/>
<TextBlock HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text=
"{Binding Endorsement.Name}" VerticalAlignment="Top" Grid.Column="1" />
<Button Command="{Binding DeselectCommand}" Content="-" HorizontalAlignment=
"Right" VerticalAlignment="Top" Width="25" Grid.Column="2"/>
</Grid>
When the page loads and when the 'Selected' is set is two different times; hence selected is null when the page loads and nothing happens. Anticipate the null situation such as an added style
<DataTrigger Binding="{Binding RelativeSource={x:Static
RelativeSource.Self}, Path=DataContext.Selected}" Value="{x:Null}">
...
You shouldn't have to do this in your DataTrigger binding:
Binding="{Binding RelativeSource={x:Static
RelativeSource.Self}, Path=DataContext.Selected}"
A binding by default is referencing the DataContext so the equivalent simpler form is this:
Binding="{Binding Path=Selected}"
I don't think that will solve your problem though (but if it does that's great). One way around it is to define a default value in the style for your Foreground if neither trigger fires:
<UserControl.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Red" />
<Style.Triggers>
...
This assumes that all your items are deselected upon load. Hope this helps.
Cheers,
Eric

Change TextBlock of custom Grid

<Grid Grid.IsSharedSizeScope="True" Name="treeGrid" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<local:LevelConverter x:Key="levelConverter" />
<HierarchicalDataTemplate ItemsSource="{Binding Items}"
DataType="{x:Type local:DirectoryRecord}">
<Grid ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="rowHeaderColumn"/>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="column1"/>
<ColumnDefinition SharedSizeGroup="column2"/>
<ColumnDefinition SharedSizeGroup="column3"/>
<ColumnDefinition SharedSizeGroup="column4"/>
<ColumnDefinition SharedSizeGroup="column5"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding Name}"></TextBlock>
<Rectangle Grid.Column="1">
<Rectangle.Width>
<MultiBinding Converter="{StaticResource levelConverter}">
<Binding Path="Level"></Binding>
<Binding ElementName="treeViewItemToMeasure" Path="ActualWidth"></Binding>
</MultiBinding>
</Rectangle.Width>
</Rectangle>
<TextBlock Grid.Column="2"
Text="{Binding LastAccessed}"></TextBlock>
<TextBlock Grid.Column="3"
Text="{Binding Files.Count}"></TextBlock>
<TextBlock Grid.Column="4"
Text="{Binding Inherited}"></TextBlock>
<Grid.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Aquamarine" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Inherited}" Value="True">
<Setter Property="Background" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView>
<!-- My binding goes here -->
</TreeView>
</Grid>
I'm trying to change the background of the TextBlock contained within my custom Grid(TreeGrid), however this code fails with XamlParseException
'TextBlock' TargetTypes does not match type of element 'Grid'.
Instead of defining a style inside a Grid.Style section, you have to declare it as the Grid.Resource Style, with the TargetType specified, i.e.
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Aquamarine" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Inherited}" Value="True">
<Setter Property="Background" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>

WPF DevExpress LookUpEdit Popup Too High

I'm using a DevExpress LookUpEdit component in a WPF UserControl. My problem is that the popup rectangle is always much bigger that the contents of the items in the PopupContentTemplate. I can't seem to find a property that governs this or maybe I'm going about using the controls improperly (?).
Thanks
<UserControl xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
x:Class="FTI_Opp_WPF.Views.UserControls.ViewSelector"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="../../Common/Styles.xaml" />
<ResourceDictionary
Source="../../Common/Strings.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" FontSize="12" Foreground="gray" Margin="0,-2,1,0">View:</Label>
<dxg:LookUpEdit
Grid.Column="1"
dx:ThemeManager.ThemeName="MetropolisLight"
Name="viewLookupEdit"
AutoPopulateColumns="False"
AutoComplete="True"
IncrementalFiltering="True"
ImmediatePopup="True"
IsReadOnly="True"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Text="{Binding SelectedViewName}"
ShowSizeGrip="false"
MinWidth="200">
<dxg:LookUpEdit.PopupContentTemplate>
<ControlTemplate>
<StackPanel Orientation="Vertical" Height="auto">
<ListBox Name="lstMain"
ItemsSource="{Binding Path=GridViews}"
HorizontalAlignment="Stretch"
BorderThickness="1"
MouseUp="OnRowMouseUp">
<ListBox.ItemContainerStyle>
<Style
TargetType="{x:Type ListBoxItem}">
<Setter Property="BorderThickness">
<Setter.Value>1</Setter.Value>
</Setter>
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type ListBoxItem}">
<Grid Name="gridView" Height="25">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Margin="0,-2,0,0"
Visibility="Visible"
Source="..\..\Images\globe.png"
Height="12"
Width="12" />
<TextBlock
Margin="2,3,0,0"
Grid.Column="1"
Name="viewName"
VerticalAlignment="Stretch"
Text="{Binding Path=Name}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger
Property="IsMouseOver"
Value="true">
<Setter
TargetName="gridView"
Property="Background" Value="{Binding Converter={StaticResource StyleConverter},ConverterParameter=HOVER_BACKGROUND_COLOR}">
</Setter>
<Setter
TargetName="viewName"
Property="Foreground" Value="White">
</Setter>
<Setter
TargetName="viewName"
Property="FontWeight" Value="BOLD">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=IsEnabled}"
Value="False">
<Setter
Property="IsEnabled"
Value="{Binding Path=IsEnabled}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button
Name="btnAdvanced"
Margin="10,0,0,0"
Style="{StaticResource Link}"
Content="manage views"
Click="OnManageViewsClick" />
</StackPanel>
</ControlTemplate>
</dxg:LookUpEdit.PopupContentTemplate>
</dxg:LookUpEdit>
</Grid>
</UserControl>
DX don't provide an "auto size to popup content" feature. You need to work with PopupMinHeight / PopupMaxHeight / PopupHeight.
See this link: http://www.devexpress.com/Support/Center/p/Q428449.aspx

DataTemplateTrigger not working with datatemplateselector

I have defined a datatemplate that has a trigger that should simply change the background color if the item is selected. For some reason it doesn't seem to be working.
<DataTemplate x:Key="existingDeviceTemplate" >
<StackPanel Orientation="Horizontal">
<Border Name="bd" Background="Green" BorderThickness="1" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=Name}" Width="200"/>
<Button Grid.Column="1" Content="Settings" Click="cmdSettings_Clicked"/>
<Button Grid.Column="2" Content="Delete" Click="cmdDelete_Clicked"/>
</Grid>
</Border>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="true" >
<Setter TargetName="bd" Property="Background" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Try to bind to the IsSelected property of the ListBoxItem:
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="true">
<Setter TargetName="bd" Property="Background" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
If your list control is a ListView you have to replace x:Type ListBoxItem with x:Type ListViewItem.
Does your binded model had a property named IsSelected?
my guess is that you mean the rows' IsSelected property.
If thats the case you need to put a RelativeSource binding with FindAncestor to the ListItem

WPF Button IsEnabled Based on ComboBox Selection Overwriting default style

I have a Button that is looking at 2 comboboxes to make sure they have a value before it is enabled. The problem is the way I am doing it is overwriting the default style declared in my theme project.
<Button x:Name="btnOK" VerticalAlignment="Center" Content="OK" IsDefault="True" Margin="0" Click="btnOK_Click">
<Button.Style>
<Style BasedOn="{StaticResource DefaultButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ddlWageTypes, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ddlJobTitles, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
I've tried adding BasedOn="{StaticResouce MyDefaultButtonStyleName}" to the style tag but it blows up at runtime.
The error is "'System.Windows.Style' value cannot be assigned to property 'Style' of object 'System.Windows.Controls.Button'. Can only base on a Style with target type that is base type 'IFrameworkInputElement'. Error at object 'System.Windows.Style' in markup file"
Is there a was to do this in XAML without overwriting default style.
EDIT: Code Sample Updated.
I get an error on OKButtonStyle saying "Cannot add element to property 'Resources', because the property can have only one child element if it uses an explicit collection tag. Error at object 'System.Windows.Style' in markup file "
<UserControl x:Class="UK.Budgeting.XBAP.ShiftDiff.NewFTEPremiumPaySummary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:compModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:local="clr-namespace:UK.Budgeting.XBAP.ShiftDiff">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CellTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style TargetType="{x:Type Button}" x:Key="OKButtonStyle" BasedOn="{StaticResource DefaultButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ddlWageTypes, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ddlJobTitles, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Rectangle Style="{StaticResource DialogRectangle}"/>
<Border Style="{StaticResource DialogBorder}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="5"/>
<ColumnDefinition MinWidth="300"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="2"/>
<RowDefinition/>
<RowDefinition Height="2"/>
<RowDefinition/>
<RowDefinition Height="2"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource LabelStyle}">Wage Type</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="2" Style="{StaticResource LabelStyle}">Job Title</TextBlock>
<ComboBox x:Name="ddlWageTypes" VerticalAlignment="Top" Grid.Column="2" Grid.Row="0"
DisplayMemberPath="DisplayName"
SelectedValuePath="WageTypeCode"/>
<ComboBox x:Name="ddlJobTitles" VerticalAlignment="Top" Grid.Column="2" Grid.Row="2"
DisplayMemberPath="JobTitle"
SelectedValuePath="JobCode"/>
<StackPanel Grid.Column="2" Grid.Row="6" VerticalAlignment="Top" Orientation="Horizontal" Margin="5">
<Button x:Name="btnOK" VerticalAlignment="Center" Content="OK" IsDefault="True" Margin="0" Click="btnOK_Click" Style="{StaticResource OKButtonStyle}"/>
<Button x:Name="btnCancel" VerticalAlignment="Center" Content="Cancel" IsCancel="True" Margin="10,0,0,0" Click="btnCancel_Click"/>
</StackPanel>
</Grid>
</Border>
</Grid>
</UserControl>
How is this
BasedOn="{StaticResouce DefaultButton}"
supposed to refer to the default button style? This crashes because DefaultButton is an undefined resource in your app.
It should be:
BasedOn="{StaticResource {x:Type Button}}"
EDIT: Sorry, answered too hastily.
I noticed now your button has a Style={} set, and is pointing to a style called OkBUttonStyle. This is the style that should define everything and be based on the default button style. By everything, I include those triggers. What you are saying in the XAML is that Style is the Content of your Button.
Maybe some code will help:
<Window x:Class="WindowsApplication7.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication7" Height="300" Width="300"
>
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="defaultButtonStyle">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type Button}" x:Key="okButtonStyle" BasedOn="{StaticResource defaultButtonStyle}">
<Setter Property="Foreground" Value="Green" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background" Value="Yellow" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Button>System default</Button>
<Button Style="{StaticResource defaultButtonStyle}">My Default</Button>
<Button Style="{StaticResource okButtonStyle}">Ok</Button>
<Button Style="{StaticResource okButtonStyle}" IsEnabled="False">Ok disabled</Button>
</StackPanel>
</Window>

Resources