I created this comboBox with checkbox-items for multiselection.
<ComboBox Height="25" HorizontalAlignment="Left" Name="MultiComboBox1" IsEditable="True" IsTextSearchEnabled="True" StaysOpenOnEdit="True" Width="240" Margin="25 0 0 0" >
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="checkBoxMulti" Width="224" IsChecked="{Binding Check_Status}" CommandParameter="{Binding Item_ID}"
Height="15" Content="{Binding Item_Name}" Checked="checkBoxMulti_Checked" Unchecked="checkBoxMulti_Unchecked">
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But im struggling to make a style out of it.
<Style x:Key="MultiComboBox" TargetType="ComboBox">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="IsEditable" Value="True"/>
<Setter Property="IsTextSearchEnabled" Value="True"/>
<Setter Property="StaysOpenOnEdit" Value="True"/>
<Style.Resources>
<ComboBox x:Key="ComboBox" x:Name="ComboBoxMulti">
<ComboBox.ItemTemplate >
<DataTemplate>
<CheckBox Name="checkBox" Width="224" IsChecked="{Binding Check_Status}" CommandParameter="{Binding Item_ID}"
Height="15" Content="{Binding Item_Name}">
<!--Checked="checkBoxMulti_Checked" Unchecked="checkBoxMulti_Unchecked">-->
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Style.Resources>
</Style>
The combobox items are not checkboxes and it doesnt show the values from "Item_Name".
Also i dont now where to use the Checked and Unchecked Events.
I have to use the multicombobox a lot, so i want to use it as a style.
Related
Is it possible to load a different data template for a defined column in a WPF data grid?
My XAML looks like this:
<DataGridTemplateColumn Header="Select">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
HorizontalContentAlignment="Center"
Visibility="{Binding IsStarted}"
VerticalAlignment="Center"
IsChecked="{Binding IsStarted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding DataContext.Checked,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
The goal here is load a separate data template when the binding IsStarted is set to false, In other words when the visibility is set to false.
The intended purpose here is when a certain button is triggered which will set the boolean to "false" another data template will be visible on this very own column instead of the currently existing items.
As an example, the following XAML should be displayed once the boolean is set to false after the execution of the button,
<TextBlock Visibility="{Binding IsTrue}" Text="Hello" />
Is this possible?
You could replace the CheckBox in the DataTemplate with a ContentControl and use a Style with a DataTrigger to replace its ContentTemplate based on the value of the IsStarted parameter:
<DataGridTemplateColumn Header="Select">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox
HorizontalContentAlignment="Center"
Visibility="{Binding IsStarted}"
VerticalAlignment="Center"
IsChecked="{Binding IsStarted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding DataContext.Checked,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding IsStarted}" Value="False">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="Some other template" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I've got a list box with SelectionMode set to Single and item template looking like this:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="100" Margin="10" Cursor="Hand" >
<Image Source="/Assets/Images/folder_80closed.png" HorizontalAlignment="Center" />
<TextBox Text="{Binding Name}" BorderThickness="0" TextAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Is there any way to change the value of image Source property in XAML based on if the item is selected or not? Something like pic bellow, where item4 is selected.
You may use an Image Style with a DataTrigger on the IsSelected property of the current ListBoxItem:
<Image HorizontalAlignment="Center">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source"
Value="/Assets/Images/folder_80closed.png"/>
<Style.Triggers>
<DataTrigger
Binding="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
Value="True">
<Setter Property="Source"
Value="/Assets/Images/some_other_image.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
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.
Our project has a DataGrid that displays an image along with text in one of the columns. When a row is selected, since it's highlighted, the image is not visible correctly. Hence we want to change the image only for the selected row.
I know how to change the property such as background of the dataGridCell using <Style.Triggers>; but cannot figure out how to change Image that is embedded inside the <DataGridTemplateColumn.CellTemplate>. Can you please help me with this?
<DataGrid Name="dgCPAGrid" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding CPAListDisplay, Mode=OneWay}" Margin="0,5,0,5" AutoGenerateColumns="False"
IsReadOnly="True" SelectionMode="Single"
SelectedItem="{Binding SelectedCPA, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False" AlternatingRowBackground="White"
HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Gray" >
<DataGrid.Resources>
<Style x:Key="DGCellMGA" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
<Setter Property="Background" Value="#f8f8d2" />
<Setter Property="TextBlock.TextAlignment" Value="Right"/>
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="{b:CaptionBinding gridHeaderCPAName}" Width="Auto" MinWidth="125">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CPAName}" />
<Image Source="/Images/TimelineIconGreenTransparent.gif" Margin="5,0,0,0" Height="15" Width="15" Visibility="{Binding StaticInd, Converter={StaticResource BoolVisConv}, ConverterParameter=inverse, Mode=OneWay}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{b:CaptionBinding gridHeaderCPAMGA}" Binding="{Binding MgaY1P}" MinWidth="50" Width="*" CellStyle="{StaticResource DGCellMGA}" />
</DataGrid.Columns>
</DataGrid>
Inside your cell's Template, add another Trigger (this time, a DataTrigger) that listens to the IsSelected property of the containing DataGridRow. Inside the DataTrigger, use the TargetName property of the Setter to tell it to modify the Source property of the Image (you have to give it an x:Name first):
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CPAName}" />
<Image x:Name="image" Source="/Images/TimelineIconGreenTransparent.gif" Margin="5,0,0,0" Height="15" Width="15" Visibility="{Binding StaticInd, Converter={StaticResource BoolVisConv}, ConverterParameter=inverse, Mode=OneWay}"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}"
Value="True">
<Setter TargetName="image"
Property="Source"
Value="/Images/Whateveryourotherimageisnamed.gif" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
I still didn't get it. Could you please show me exactly how to override ListBox's default behavior.
Everytime when ListBoxItem is selected the Border's background should be changed. Not the background of the whole row but only background of the border which's specified.
<ListBox ItemsSource="{Binding Source={StaticResource AssetsViewSource}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="Black">
<StackPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Use the DataTemplate's Triggers collection, with a RelativeSource to get you to the containing ListBoxItem:
<DataTemplate>
<Border BorderThickness="2" BorderBrush="Black" Name="Bd">
<StackPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
<DataTemplate.Triggers>
<DataTrigger Value="True"
Binding="{Binding
IsSelected,
RelativeSource={RelativeSource
AncestorType={x:Type ListBoxItem}}}">
<!-- everybody loves HotPink -->
<Setter TargetName="Bd" Property="Background" Value="HotPink"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Simply add the following into the ListBox Item tag
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ListBox.Resources>
That should do the trick..