i have a dxg:GridControl in which i have dxg:GridColumn and on every row there is a Display and Edit template like below
<dxg:GridControl>
<dxg:GridControl.Columns>
<dxg:GridColumn Width="101"
AllowBestFit="True"
AllowColumnFiltering="True"
AllowSorting="False"
FieldName="Tolerance"
Header="Tolerance Type"
ReadOnly="{Binding IsReadOnly}">
<dxg:GridColumn.EditTemplate>
<ControlTemplate>
<dxe:ComboBoxEdit
x:Name="PART_Editor"
Width="100"
ItemsSource="{Binding Path=DataContext.ToleranceTypeCollection,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding RowData.Row.Tolerance}"
DisplayMember="ToleranceType"
FilterCondition="Contains"
ImmediatePopup="True"
IncrementalFiltering="True"
IsTextEditable="False"
ValueMember="ToleranceTypeId"
ShowSizeGrip="True"
TextWrapping="WrapWithOverflow"
Style="{DynamicResource DXEComboBoxEditStyle}" />
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.ToleranceTypeCollection.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="0">
<Setter TargetName="PART_Editor" Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding DataContext.IsReadOnly, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="True">
<Setter TargetName="PART_Editor" Property="IsEnabled" Value="False" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</dxg:GridColumn.EditTemplate>
<dxg:GridColumn.DisplayTemplate>
<ControlTemplate>
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center" Text="{Binding RowData.Row.Tolerance.ToleranceType}" />
</ControlTemplate>
</dxg:GridColumn.DisplayTemplate>
</dxg:GridColumn>
</dxg:GridControl.Columns>
</dxg:GridControl>
the values are coming well on the Display template but while clicking on Cell, the control is changing to dxe:ComboBoxEdit but it is not automatically picking the saved value from List of values in drop down hence the value on Display templates's textbox is being lost after clicking somewhere else.
Using Dev Express v 18.1
Please help on the same
Thank you
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.
I don't have that much experiences in WPF and XAML files, and maybe my question is very simple. so let me get your help if possible.
As a brief, it's a survey system that will go through some questions, where each question has exactly three answers. Answers are in a ListBox.
In order to make it easier for illiterate people, I want that the ToogleButton in each answer to have different shape: 1st is square, 2nd is Circle and 3rd is Triangle. my objective is to set the shape based on the ListBoxItem index.
Code is below with a static shape (Currently Triangle):
<ListBox x:Name="AnswersListBox" Margin="0 16 0 16" ItemsSource="{Binding Path=CurrentQuestion.Answers, ElementName=Questionctl}" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DAL:Answer}">
<Grid Height="85">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="300*"/>
</Grid.ColumnDefinitions>
<ToggleButton Margin="10 0" Style="{StaticResource MaterialDesignActionToggleButton}" ToolTip="Click to Select" VerticalAlignment="Center"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Viewbox Width="32" Height="32">
<Canvas Width="32" Height="32">
<Path Data="M12,2L1,21H23M12,6L19.53,19H4.47" Width="32" Height="32" Stretch="Fill"
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToggleButton}}, Path=Foreground}"/>
</Canvas>
</Viewbox>
</ToggleButton>
<TextBlock Text="{Binding Path=AnswerID, Converter={StaticResource AnswerTextConverter}}" VerticalAlignment="Center" TextWrapping="Wrap"
Foreground="#424242" Grid.Column="1" FontSize="24"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to change the property Data of the object Path based on the Main Container's index which is a ListBoxItem. Most probably, I have to use triggers to set the Data value based on the index number of the ListBoxItem, but unable to do that...
Is that possible?
In ListBox, there are no direct properties to get the index of the item. However, the same can be achieved by setting the AlternationCount and accessing AlternationIndex.
<ListBox x:Name="AnswersListBox"
Margin="0 16 0 16"
ItemsSource="{Binding Path=Answers}"
Grid.Row="1"
AlternationCount="100">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DAL:Answer}">
<Grid Height="85">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="300*"/>
</Grid.ColumnDefinitions>
<ToggleButton Margin="10 0" Style="{StaticResource MaterialDesignActionToggleButton}" ToolTip="Click to Select" VerticalAlignment="Center"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Viewbox Width="32" Height="32">
<Canvas Width="32" Height="32">
<Path Width="32" Height="32" Stretch="Fill"
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToggleButton}}, Path=Foreground}">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Data" Value="M12,2L1,21H23M12,6L19.53,19H4.47"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}" Value="1">
<Setter Property="Data" Value="M12,2L1,21H23M12,6L19.53,19H4.47"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}" Value="2">
<Setter Property="Data" Value="M12,2L1,21H23M12,6L19.53,19H4.47"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</Canvas>
</Viewbox>
</ToggleButton>
<TextBlock Text="{Binding Path=AnswerID, Converter={StaticResource AnswerTextConverter}}" VerticalAlignment="Center" TextWrapping="Wrap"
Foreground="#424242" Grid.Column="1" FontSize="24"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Replace the Value of Data with the required shape.
Many Thanks to #Subramaniam B, and based on his Answer which requires a few changes in the binding of the DataTrigger where it should be:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Data" Value="M28.3,28.3H0L14.2,0L28.3,28.3z M2.9,26.6h22.5L14.2,4L2.9,26.6z"/>
</DataTrigger>
and the full code will be:
<ListBox x:Name="AnswersListBox" AlternationCount="3">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DAL:Answer}">
<ToggleButton Style="{StaticResource myCheckboxToggleButtonWithInsideCheck}" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=(ItemsControl.AlternationIndex)}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Viewbox Width="60" Height="60">
<Canvas Width="32" Height="32">
<Path Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Label}}, Path=Foreground}" Height="32" Stretch="Fill" Width="32">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Data" Value="M16.2,28.3h-4.1C0.5,28.3,0,27.9,0,16.2v-4.1C0,0.5,0.5,0,12.1,0h4.1c11.7,0,12.1,0.5,12.1,12.1v4.1C28.3,27.9,27.9,28.3,16.2,28.3z M12.1,1.9C7,1.9,4.1,1.9,3,3s-1.1,4-1.1,9.1v4.1c0,5.2,0,8,1.1,9.1c1.1,1.1,4,1.1,9.1,1.1h4.1c5.2,0,8,0,9.1-1.1c1.1-1.1,1.1-4,1.1-9.1v-4.1c0-5.2,0-8-1.1-9.1c-1.1-1.1-4-1.1-9.1-1.1H12.1z"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="1">
<Setter Property="Data" Value="M28.3,28.3H0L14.2,0L28.3,28.3z M2.9,26.6h22.5L14.2,4L2.9,26.6z"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="2">
<Setter Property="Data" Value="M14.2,28.3C6.4,28.3,0,22,0,14.2S6.4,0,14.2,0s14.2,6.4,14.2,14.2S22,28.3,14.2,28.3z M14.2,2.2c-6.6,0-12,5.4-12,12c0,6.6,5.4,12,12,12c6.6,0,12-5.4,12-12C26.2,7.6,20.8,2.2,14.2,2.2z"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</Canvas>
</Viewbox>
</ToggleButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
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>
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>