WPF can't open Combobox - wpf

I used trigger to show the combobox when selected row, but it can't open.
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<ComboBox Grid.Column="1" Height="24" Cursor="Hand"
Style="{StaticResource ComboBoxGridStyle}"
ItemsSource="{Binding Path=ListItem, Mode=TwoWay}"
DisplayMemberPath="Name" SelectedValuePath="Key"
SelectedValue="{Binding Path=ListItem, Mode=TwoWay}"
SelectedItem="{Binding Path= ItemSelect, Mode=TwoWay}"
IsDropDownOpen="False">
</ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>

Related

WPF Select ComboBox text style based on variable

I have a ComboBox and I'd like to be able to change the style of the text inside the ComboBoxItem based on an attribute of the ComboBoxItem.
<ComboBox ItemsSource="{Binding Countries}">
<ComboBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" Focusable="False">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource MyStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ComboBox.ItemTemplate>
I've been trying to set the TextBlock Style from MyStyle to a different one if the Country's attribute IsWestern is true, but I am not managing. I figure I need a Trigger somewhere, but I can't figure it out and added a variety, but none worked so far.
use DataTrigger to change ContentTemplate:
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource MyStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWestern}" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource WesternStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
alternatively include <DataTrigger Binding="{Binding Path=IsWestern}" Value="True"> in TextBlock Style (MyStyle) and change required TextBlock properties there:
<Style x:Key="MyStyle" TargetType="TextBlock">
<Setter Property="LineStackingStrategy" Value="BlockLineHeight"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWestern}" Value="True">
<Setter Property="Padding" Value="20,0,0,0"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>

wpf ComboBox can't get focus Move by Tab Key

It's my GUI.
When I focus on TextBox, I can move focus by Tab key.
But when I focus on Tel No. Text Box, I press Tab Key but focus just stay and not move.
I want to move focus to ComboBox(editable) by pressing Tab key.
.xaml file
<TextBlock Text="{Resx incidentCallName }" Grid.Row="2" Style="{DynamicResource TextBlockSubStyle}"/>
<TextBox x:Name="txtbox_who" Grid.Row="2" Grid.Column="1" Style="{DynamicResource TextInputStyle}" Validation.Error="Validation_Error"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Person, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>
<TextBlock Text="{Resx incidentCallNo}" Grid.Row="3" Style="{DynamicResource TextBlockSubStyle}"/>
<TextBox x:Name="txtbox_tel" Grid.Row="3" Grid.Column="1" Style="{DynamicResource TextInputStyle}" LostFocus="txtbox_tel_LostFocus" PreviewKeyDown="txtbox_tel_PreviewKeyDown" Validation.Error="Validation_Error"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Tel, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>
<Button x:Name="btn_history" IsTabStop="True" Grid.Row="3" Grid.Column="2" Content="!" Margin="1" IsEnabled="False" Click="btn_history_Click" FontSize="18"/>
<TextBlock Text="{Resx incidentList_type}" Grid.Row="4" Style="{DynamicResource TextBlockSubStyle}"/>
<ComboBox x:Name="cb_type" Grid.Row="4" Grid.Column="1" Style="{DynamicResource ComboBoxStyle}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=TypeCombo, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Code_name" SelectedValuePath="Comm_code_id" IsEditable="True" IsReadOnly="True" Validation.Error="Validation_Error"
SelectedValue="{Binding UpdateSourceTrigger=PropertyChanged, Path=TypeCode, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.CbTypeSelect, UpdateSourceTrigger=PropertyChanged}"/>
App.xaml (for style)
<Style x:Key="ComboBoxStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
<Style.Triggers>
<Trigger Property="IsSelectionBoxHighlighted" Value="True">
<Setter Property="IsDropDownOpen" Value="true" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsDropDownOpen" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
What is the problem? Please help me.

drop down should appear only on certain rows of listview wpf MVVM pattern

My styles and resources
<Window.Resources>
<ConvertorObj:BoolToVisibilityConverter x:Key="boolToVis"/>
<Style TargetType="{x:Type TextBlock}" x:Key="GridBlockStyle">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Visibility"
Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}},
Converter={StaticResource boolToVis}, ConverterParameter=False}" />
<Setter Property ="FontFamily" Value="Verdana" ></Setter>
<Setter Property ="FontSize" Value="14" ></Setter>
</Style>
<Style TargetType="{x:Type FrameworkElement}" x:Key="GridEditStyle">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Visibility"
Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}},
Converter={StaticResource boolToVis}, ConverterParameter=True}" />
<Style.Triggers>
<DataTrigger>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
These are the two types of styles applied, GridBlockStyle and GridEditStyle. In a normal view the rows should behave like textblocks. When I edit the row it should behave as textbox with a combobox. The problem is that I get comboboxes in all the rows of the listview. it should appear only in certain rows of my listview.
My listview is as follows
<ListView Margin="10" Name="ListValues" AlternationCount="2"
ItemsSource="{Binding Path= ListValues}"
ScrollViewer.VerticalScrollBarVisibility="Auto" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#00B2EE">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="200">
<GridViewColumnHeader Tag="Name" Content="Name" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Name}" Style="{StaticResource GridBlockStyle}" />
<TextBlock Text="{Binding Path=Name}" Style="{StaticResource GridEditStyle}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="Auto">
<GridViewColumnHeader Tag="Settings" Content="Settings" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="240"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Style="{StaticResource GridBlockStyle}"
Text="{Binding Path=Settings}"/>
<TextBox Grid.Column="0"
Text="{Binding Path=Settings}"
FontFamily="verdana" FontSize="14"
MaxLength="31" Width="240"
Style="{StaticResource GridEditStyle}"/>
<TextBlock Grid.Column="1" Style="`{StaticResource GridBlockStyle}" />`
<ComboBox Grid.Column="1"
Style="{StaticResource GridEditStyle}"
ItemsSource="{Binding Path=CmbSettings}"
SelectedItem="{Binding Path=CmbSelectType}"
IsEnabled="{Binding Path = IsComboAvailable}"
Grid.RowSpan="2"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Sample view model
_sampleModel = new SampleModel();
_sampleModel.Name = "test1";
_sampleModel.Settings = "aa";
_sampleModel.IsComboAvailable = false;
ListValues.Add(_sampleModel);
_sampleModel = new SampleModel();
_sampleModel.Name = "test1";
_sampleModel.Settings = "3";
_sampleModel.CmbSelectType = "Second";
_sampleModel.CmbSettings.Add("Second");
_sampleModel.CmbSettings.Add("Minute");
_sampleModel.IsComboAvailable = true;
ListValues.Add(_sampleModel);
ListValues.Add(_sampleModel);
Currently on editing the row the disabled dropdown is visible. It should be removed.
When IsEnabled is false, a ComboBox will appear grayed out and not be editable. However, since you have located the TextBlock and the ComboxBox in the same grid column, and the ComboBox is declared later, your TextBlock is still covered by the ComboBox even when the ComboBox is disabled. What you want to do is set the Visibility to Collapsed when IsComboAvailable is false.
Try this:
<ComboBox Grid.Column="1" Name="combo"
...
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsComboAvailable}" Value="False">
<Setter TargetName="combo" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>

Why can't I use AlternationCount on a Border?

In the code below if I change
<Style TargetType="{x:Type Border}">
To
<Style TargetType="{x:Type ListBoxItem}">
It will color my entire listboxitem the correct color. But I only want to hit the border background
<ListBox x:Name="FilteredMessagesListBox" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch" SelectionMode="Extended" Background="Transparent" AlternationCount="2">
<ListBox.Resources>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightBlue"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGreen"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate >
<DataTemplate>
<DockPanel Margin="0,0,0,3">
<Button x:Name="AttachmentImageButton" Click="AttachmentImageButton_Click" DockPanel.Dock="Bottom" MaxWidth="200" MaxHeight="200" HorizontalContentAlignment="Center" Visibility="{Binding ElementName=AttachmentImageButton, Converter={StaticResource cImageAttachmentToVisible}}" >
<Image Source="{Binding Path=Attachment}" x:Name="AttachmentImage" />
</Button>
<Button x:Name="AttachmentButton" Click="AttachmentButton_Click" DockPanel.Dock="Right" Visibility="{Binding ElementName=AttachmentButton, Converter={StaticResource cAttachmentToVisible}}" >
<Image Source="/MobilWPF;component/Resources/Images/PaperClip/PaperClip.jpg" Width="20" Height="20"/>
</Button>
<TextBlock Text="{Binding Converter={StaticResource cGetInstantMessageHeader}}" Width="120" TextWrapping="Wrap" HorizontalAlignment="Left" Background="Transparent" FontSize="10" DockPanel.Dock="Left"/>
<Border DockPanel.Dock="Left" HorizontalAlignment="Stretch" CornerRadius="5">
<DockPanel>
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Message}" TextWrapping="Wrap" DockPanel.Dock="Left" HorizontalAlignment="Left" Background="Transparent" FontSize="12"/>
</DockPanel>
</Border>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I tried this:
<Border DockPanel.Dock="Left" HorizontalAlignment="Stretch" CornerRadius="5">
<Border.Triggers>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
<Setter Property="Background" Value="LightBlue"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="1">
<Setter Property="Background" Value="LightGreen"/>
</DataTrigger>
</Border.Triggers>
<DockPanel>
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Message}" TextWrapping="Wrap" DockPanel.Dock="Left" HorizontalAlignment="Left" Background="Transparent" FontSize="12"/>
</DockPanel>
</Border>
Cannot find the static member 'BackgroundProperty' on the type 'ContentPresenter'
The problem is that AlternationIndex is set on the containers (ListBoxItems in this case), not on some child within them. You could solve this by using DataTriggers that bind to AlternationIndex on the parent ListBoxItem instead:
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
<Setter Property="Background" Value="LightBlue"/>
</DataTrigger>

How to apply style trigger to datatemplate in WPF

I've got the following..
<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" Margin="85,2,0,2">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBox AcceptsReturn="True" Width="200" Height="100"/>
<DataTemplate.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsReadOnly" Value="True">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="False"/>
</Style.Triggers>
</Setter>
</Style>
</DataTemplate.Resources>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The problem is that you can't apply a Style.Trigger like I'm trying to do inside a DataTemplate. So my question is how would you apply create a trigger so that a property on the DataTemplate changes based on the parent?
FINAL SOLUTION:
I took what Souvik gave me and fixed it up since there were a few problems. Here is the end result.
<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" DisplayMemberPath="Value" Margin="85,2,0,2">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBox AcceptsReturn="True" Width="200" Height="100" Text="{Binding Path=Value}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=IsEditable}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEditable" Value="True"/>
<Style.Triggers>
<Trigger Property="IsDropDownOpen" Value="True" >
<Setter Property="IsEditable" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Resources>
Have DataTemplate trigger instead of Style trigger:
<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" Margin="85,2,0,2">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBox AcceptsReturn="True" Width="200" Height="100"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Resources