Radcombobox loses value when clicked on another control - wpf

Two comboboxes - the user makes a selection in the first which provides a default value for the 2nd. This works well until a series of keystrokes happen. If the 2nd gets the focus, but not changed, then the first gets the focus, the default value disappears for the 2nd.
What would cause the default value disappear?
I suspect the second combo is not set up correctly but I'm not sure how to fix it.
<telerik:RadComboBox x:Name="cboGlaze" FontSize="16" Background="#F6F8FA" BorderBrush="#D7D8DD"
ItemsSource="{Binding}"
Text="{Binding glaze, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
telerik:TextSearch.TextPath="Glaze"
IsEditable="True" IsReadOnly="True"
Style="{DynamicResource RadComboBoxStyle3}" >
<telerik:RadComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Glaze}"></TextBlock>
<TextBlock Text="{Binding pctUpcharge}" Grid.Column="1"></TextBlock>
</Grid>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
Binding to the property "glaze" is handled in the DropDown event for the first combo.
Thanks for any help or advice.

Try to bind the SelectedValue property of the RadComboBox to the glaze source property and set the SelectedValuePath property to "Glaze" provided that the DataTable contains a column with this name:
<telerik:RadComboBox x:Name="cboGlaze" FontSize="16" Background="#F6F8FA" BorderBrush="#D7D8DD"
ItemsSource="{Binding}"
SelectedValuePath="Glaze"
SelectedValue="{Binding glaze}"
telerik:TextSearch.TextPath="Glaze"
IsEditable="True" IsReadOnly="True"
Style="{DynamicResource RadComboBoxStyle3}" >
...

Related

How to pass the right Context to a DataTemplate within an ItemTemplate of a WPF ListView [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
in my WPF app there is a ListView defined as follows:
<ListView
HorizontalContentAlignment="Stretch"
DockPanel.Dock="Top"
Grid.IsSharedSizeScope="True"
ItemsSource="{Binding Path=ListSource}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="ParameterIconSize" />
<ColumnDefinition
Width="Auto"
MaxWidth="100"
SharedSizeGroup="ParameterDescriptionSize" />
<ColumnDefinition
Width="Auto"
MinWidth="40"
SharedSizeGroup="ParameterValueSize" />
</Grid.ColumnDefinitions>
<Image
Grid.Column="0"
MaxHeight="50"
Source="{Binding Path=Icon}" />
<ContentPresenter
Grid.Column="2"
Content="{Binding Path=RenderValue, Converter={StaticResource RenderObjToValueConverter}}"
ContentTemplateSelector="{StaticResource ResourceKey=RenderTemplateSelector}"
/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I've defined everything in the Resources as follows:
<!-- TEXT -->
<DataTemplate x:Key="TextValueTemplate">
<DockPanel DataContext="{StaticResource VM}">
<TextBlock
MaxWidth="75"
Margin="5,0"
VerticalAlignment="Center"
Background="Red"
DockPanel.Dock="Top"
FontSize="14"
Text="{Binding Path=Description, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
TextTrimming="CharacterEllipsis">
<TextBlock.ToolTip>
<TextBlock
MaxWidth="100"
Text="{Binding Path=Description, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap" />
</TextBlock.ToolTip>
</TextBlock>
</DockPanel>
</DataTemplate>
<!-- IMAGE -->
<DataTemplate x:Key="ImageValueTemplate">
<DockPanel DataContext="{StaticResource VM}">
<Image MaxHeight="50" Source="{Binding Path=CurrentValueImage}" />
</DockPanel>
</DataTemplate>
<!-- COMBO BOX -->
<DataTemplate x:Key="ComboValueTemplate">
<DockPanel DataContext="{StaticResource VM}">
<TextBlock
Grid.Column="1"
MaxWidth="75"
Margin="5,0"
VerticalAlignment="Center"
FontSize="14"
Text="{Binding Path=Description, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
TextTrimming="CharacterEllipsis">
<TextBlock.ToolTip>
<TextBlock
MaxWidth="100"
Text="{Binding Path=Description, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap" />
</TextBlock.ToolTip>
</TextBlock>
<ComboBox/>
</DockPanel>
</DataTemplate>
<sel:ParameterVisualizationTemplateSelector
x:Key="RenderTemplateSelector"
ComboTemplate="{StaticResource ResourceKey=ComboValueTemplate}"
GraphicTemplate="{StaticResource ResourceKey=ImageValueTemplate}"
TextTemplate="{StaticResource ResourceKey=TextValueTemplate}" />
Everything compiles and runs, but then I am not able to correctly pass the ListViewItem to the inner datatemplate.
What I want to do is to be able to change what is shown based on some logic (not shown here) between an image, some text and a combobox. What's going inside those controls is defined in the ListViewItem. As you can see, I tried to write a converter that return the corresponding type (image, text, list of data), but I am missing the link between the various DataTemplates.
FINAL EDIT: Based on the answer from #mm8 I found that the real issue was not passing the context, but that the property passed wasn't correctly processed and then no value would be set up whilst not giving any binding error.
When you set DataContext="{StaticResource VM}" in your template, you are effectively "overriding" the default DataContext which is the current item in the ListSource source collection of the ListView.
So, if I understand correctly, the "dependency" chain is something like control <- textItemtemplate <- ListViewItemTemplate <- Viewmodel, so the final control automatically "inherits" the object?
You bind the ItemsSource of the ListView to a source collection property called ListSource:
<ListView ... ItemsSource="{Binding Path=ListSource}">
The default DataContext of the root element (the Grid in this case) in the ListView's ItemTemplate will then be an element in ListSource.
This means that if ListSource returns an IEnumerable<T> you can bind directly to any public property of the type T in the ItemTemplate provided that you don't explictly set the DataContext property somewhere in the template.
So Icon is supposed to be a property of T (whatever your type T is) in your example:
<Image Grid.Column="0"
MaxHeight="50"
Source="{Binding Path=Icon}" />
If you for example remove DataContext="{StaticResource VM}" from the TextValueTemplate, the binding to Description should work provided that there is a Description property defined in the object returned from the converter of the RenderValue property.

Retrieving selected item from listbox

How do i retrieve values from selected item in listbox.
I have a databound listbox:
<ListBox x:Name="EventsListBox"
ItemsSource="{Binding}"
SelectedValuePath="idevents"
ItemTemplate="{DynamicResource DataTemplate2}"
/>
Which uses this datatemplate to populate:
<DataTemplate x:Key="DataTemplate2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="0,5,5,5" Grid.ColumnSpan="2">
<DockPanel TextBlock.FontWeight="Bold" >
<TextBlock Text="{Binding Path=eventname, FallbackValue=Manchester}" HorizontalAlignment="Center"/>
</DockPanel>
<TextBlock Text="{Binding Path=realchannelname, FallbackValue=Channel}" FontStyle="Italic" HorizontalAlignment="Left" />
<TextBlock Text="{Binding Path=datetime, FallbackValue=datetime}" HorizontalAlignment="Left" />
</StackPanel>
</Grid>
</DataTemplate>
How would i get 'eventname' value and set it as a string to a label here;
Private Sub EventsListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles EventsListBox.SelectionChanged
End Sub
In general, I try to do things through binding because they will automatically update.
In Code Behind you can gain access to the value of the item via the SelectedValue property.
String value = EventsListBox.SelectedValue;
A better way of doing this is to have a binding update a property. This way everything always will update with bindings
<ListBox x:Name="EventsListBox"
SelectedValue="{Binding SelectedEventName} >
</ListBox>
You should note that the SelectedEventName is a property on the viewmodel that the listbox is bound to.

wpf Combobox Itemssource not binding within Listbox Data Template

Please help to fix the below issue.
I have Combobox inside listbox item datatemplate so that I can load multiple combobox but same itemsource
<StackPanel>
<ListBox Width="400" Name="lstFiles" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="dataGrid">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Name="dragFileName"
Margin="5,0,0,0"
Text="{Binding fileName, UpdateSourceTrigger=Default}" />
<ComboBox Grid.Row="0"
Grid.Column="1"
Margin="5,0,0,0"
Name="cboDragDocType"
ItemsSource="{Binding dragDocType, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"
Text="{Binding dragDocTypeText}"
IsEditable="True"
IsReadOnly="True" />
<Button Name="dragDelBtn"
Grid.Column="2"
Height="20">X</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Data is loading in comboBox by using ObservableCollection property. In Initialize method i do for loop method to get multiple items.
Problem scenario: Select item from the dropdown1 --> then Select item from the dropdown2 -->Again click dropdown1, Now dropdown1 itemssource is empty.
You don't have a binding to the SelectedItem (or SelectedValue) property of the ComboBox. Therefore the value cannot be saved (or retrieved).
You need to add a property in your class that acts as the DataContext to hold the selected item of the ComboBox, for example:
<ComboBox Grid.Row="0"
...
SelectedItem="{Binding Path=SelectedDragDocType, UpdateSourceTrigger=LostFocus}" />
As a side-note having both IsEditable and IsReadOnly properties on the ComboBox set to true is pretty restrictive. Are you sure it is the intended behavior?

Set Text on combobox while getting data

I'm trying to display a default text in a ComboBox while I'm fetching data from a source, but it doesn't show anything.
<ComboBox
Grid.Row="1"
Grid.Column="2"
Text="Hepper"
ItemsSource="{Binding Builds}"
SelectedItem="{Binding SelectedBuild}"
DisplayMemberPath="VersionNo"
IsReadOnly="True"
IsEnabled="{Binding SelectedBuildEnable}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="180"
Height="30"
MinWidth="180" />
you can try to set the ComboBox.SelectedValue Property instead of ComboBox.Text.
I prefer to show another TextBlock above the ComboBox to display a default text:
<!-- don't forget to define the converter in your resources -->
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
<!-- your Control -->
<ComboBox
Grid.Row="1"
Grid.Column="2"
x:Name="ComboBoxElement"
ItemsSource="{Binding Builds}"
SelectedItem="{Binding SelectedBuild}"
DisplayMemberPath="VersionNo"
IsReadOnly="True"
IsEnabled="{Binding SelectedBuildEnable}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="180"
Height="30"
MinWidth="180" />
<TextBlock
Grid.Row="1"
Grid.Column="2"
Visibility="{Binding IsEnabled, ElementName=ComboBoxElement, Converter={StaticResource BooleanToVisibilityConverter}}"
IsHitTestVisible="False"
Text="Hepper"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="15,5" />
I guessed that your ComboBox becomes enabled if the data is fetched. Otherwise you have to use another binding for the visibility.
According to MSDN, the ComboBox.Text property
Gets or sets the text of the currently selected item.
Therefore, you could temporarily add an item into your ComboBox with your required message, select it and then remove it before filling the ComboBox when your data arrives.

How to get selected item value?

How to get ItemsControl display items selected Item textbox text value using MVVM pattern?
<ListBox Margin="0,25,0,0" Grid.Row="3" ItemsSource="{Binding Path=ViewModelSearchResults}" SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=CategoryName}" FontSize="14" FontWeight="Normal" />
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=CategoryID}" FontSize="14" FontWeight="Normal" Visibility="Hidden" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
Like getting anything else from Views: bind it! Yes, ItemsControl has SelectedItem but ItemsControl itself doesn't have selection behavior. You should use something like ListBox instead.
You can bind SelectedItem to a property and access the required values through that property.

Resources