WPF - Select ComboBox value by typing when using DataTemplate - c#

I have a combobox in wpf which is binded to some property (another object). Because I need to show two properties of that object i used DataTemplate inside combobox. Now when combobox is in focus I can't select some value by typing few starting letters (without DataTemplate it is possible).
<ComboBox Height="23" HorizontalAlignment="Left" Margin="104,14,0,0" Name="tipDokumentaCombo" VerticalAlignment="Top" Width="241" TabIndex="0" ItemsSource="{Binding Path=TipoviDokumenta}" SelectedValue="{Binding Path=Potvrda.Tip}" SelectedValuePath="Tip" SelectionChanged="tipDokumentaCombo_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Tip}" />
<TextBlock Text=" (" />
<TextBlock Text="{Binding Path=OpisDokumenta}" />
<TextBlock Text=")" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Set TextSearch.TextPath to the property that should be searched.
Update
Because the abvove solution seems not to work for you, try to set the search text manually for the container:
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="TextSearch.Text" Value="{Binding Tip}" />
</Style>
</ComboBox.ItemContainerStyle>

Add DisplayMemberPath to the property on which lookup should work on.
Setting DisplayMemberPath worked for me even when itemtemplate is present.

Based on the comment discussion, the solution that works is to add both IsTextSearchEnabled="True" and TextSearch.TextPath="Tip" in the ComboBox tag.
For example (re-writting the question code sample - removing some not useful to the example code to reduce complexity)
<ComboBox Name="tipDokumentaCombo" TabIndex="0" ItemsSource="{Binding Path=TipoviDokumenta}" SelectedValue="{Binding Path=Potvrda.Tip}" SelectedValuePath="Tip" SelectionChanged="tipDokumentaCombo_SelectionChanged" IsTextSearchEnabled="True" TextSearch.TextPath="Tip">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Tip}" />
<TextBlock Text=" (" />
<TextBlock Text="{Binding Path=OpisDokumenta}" />
<TextBlock Text=")" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Disclaimer: I am not the original author. This answer is fabricated from the existing comments. Attribution should be given to #mersadk who post, most of this answer's details in the comments. I test it and verified that this works in my (similar) environment - issue.

Related

WPF Binding TextBlock value to display SelectedItem in ComboBox

I have a ComboBox and a couple of TextBlock fields.
I want to display the properties of a SelectedItem from the ComboBox on those Textblock's. Image
So that when I choose one of multiple user's the properties in the TextBlock will update to those of the SelectedItem. I have found an example, although it is using silverlight, and does not work entirely.
<ComboBox Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Bottom"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
Margin="0"
Height="40"
Name="ComboBox"
ItemsSource="{Binding UserModels}"
SelectedItem="{Binding EnteredUserModel, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"
Style="{StaticResource ResourceKey=ComboBoxItemTextBlock}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<TextBlock Grid.Row="1"
Grid.Column="0"
Margin="0 10 0 10" >
<Run Text="{DynamicResource firstName}" />
<Run Text=": " />
<Run Text="{Binding ElementName=ComboBox, Path=SelectedItem, UpdateSourceTrigger=PropertyChanged}" />
</TextBlock>
This is what I've tried. I added Name to the ComboBox so that I can access it's SelectedItem in my TextBlock. I need to get the SelectedItem.firstname, etc. At this stage i can only access the entire objects.
Am I missing some useful binding?
In order to show the FirstName property of the SelectedItem, just use an appropriate property path, i.e. SelectedItem.FirstName:
<Run Text="{Binding ElementName=ComboBox, Path=SelectedItem.FirstName}" />
or, since SelectedItem is bound to an EnteredUserModel property in your view model:
<Run Text="{Binding Path=EnteredUserModel.FirstName}" />
Setting UpdateSourceTrigger=PropertyChanged is not necessary. It has no effect in a OneWay Binding.
You're getting the EnteredUserModel-Object, because that's the selected item of the ComboBox. If you want the displayed text you must bind to the FirstName-Property.
Alternatively you can bind to EnteredUserModel.FirstName in your TextBox

How to remove header space on ComboBox in XAML

I'm trying to line up a TextBox and a ComboBox inside of a StackPanel, but the ComboBox seems to have some extra space at the top of it that I can't get rid of.
How can I get these two controls to align with each other properly?
<StackPanel Orientation="Horizontal">
<TextBox IsTextPredictionEnabled="False" Margin="0,0,0,0" BorderBrush="Gray"
Text="{Binding ZoneName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PlaceholderText="YourCompany"/>
<ComboBox VerticalAlignment="Stretch" BorderBrush="Gray" Padding="0,0,0,0" Margin="0,0,0,0"
ItemsSource="{Binding Path=DomainChoices}"
SelectedValue="{Binding Path=SelectedDomainChoice, Mode=TwoWay}"
SelectedValuePath="{Binding id}"
DisplayMemberPath="{Binding text}"
Foreground="Black" Width="200">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding text}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
When you take a look at ComboBox's style then you will find that <Border x:Name="ShortListOuterBorder" .... has a Margin with value of Margin="{ThemeResource PhoneTouchTargetOverhang}". This resource is defined in the style above:
<Thickness x:Key="PhoneTouchTargetOverhang">0,9.5</Thickness>
You can easily define your own style in which you can change the above value, or change margin of the Border. You can also try to set the margin of your ComboBox to 0,-9.5,0,0 without applying the style.

Easiest way to access Texblock inside listbox itemtemplate

I have seen a lot of questions and answers with almost the same problem, but none of these answers arent working for me. Soo, my code is:
<ListBox ItemsSource="{Binding Avakuvaandmed}" x:Name="lboxandmed" HorizontalAlignment="Left" Height="552" VerticalAlignment="Top" Width="970" SelectionChanged="lboxandmed_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="spanVärviSeda">
I HAVE TO GET VALUE OF THIS --> <TextBlock x:Name="IDbox" Width="50" Text="{Binding Id}"></TextBlock>
<TextBlock Width="130" Text="{Binding Nrmärk}"></TextBlock>
<TextBlock x:Name="txtKehtivus" Width="130" Text="{Binding Lõpp}"></TextBlock>
<TextBlock Width="130" Text="{Binding Eesnimi}"></TextBlock>
<TextBlock Width="130" Text="{Binding Perenimi}"></TextBlock>
<TextBlock Width="130" Text="{Binding Mark}"></TextBlock>
<TextBlock Width="130" Text="{Binding Mudel}"></TextBlock>
<TextBlock Width="130" Text="{Binding Aasta}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I have to get the value of the textblock named "IDbox".
Please can someone help me, or atleast give me a clue how.
Your code looks correct. If you want to access the Value of IDbox in code behind then you can do it by Avakuvaandmed.ElementAt(rowno).Id because you are binding Id to the IDBox. If you want to access the BoxId value in xaml. Then use binding as follows:
{Binding Avakuvaandmed[rowno],Path=Id}
You can also access Textblock value by using VisualTreeHelper class. You will need to go traverse all elements in ListBox.

DependencyProperty Binding Does Not Update To Collection CurrentItem

I am trying to bind a dependency property to a collection's current selection and for reasons I can't seem to grasp, the binding does not update when the collection changes.
In the example below, I show two example. One is updating correctly (on the textblock/run), and the other only displays the initial element and doesn't change when the data grid selection changes.
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding Path=List}" x:Key="myViewModel"/>
<my:UpdateNotWorking MyObjModel="{Binding Source={StaticResource myViewModel}, Path=CurrentItem}" x:Key="updateNotWorking" />
</Grid.Resources>
<DataGrid ItemsSource="{Binding Source={StaticResource myViewModel}}" Name="mylistbox"/>
<TextBlock TextWrapping="Wrap" FontWeight="Bold" Foreground="#FF50CEFF" FontSize="24" TextAlignment="Center" Height="75">
<Run Text="{Binding Source={StaticResource myViewModel}, Path=text}" Foreground="#FF00E200" />
</TextBlock>
<TextBox Text="{Binding Source={StaticResource updateNotWorking}, Path=MyObjModel.text}" Height="22"/>
</Grid>
My dependency property in this example is "MyObjModel" on the "UpdateNotWorking" dependency object which is instantiated from the xaml code.
I would appreciate any information as to why my property is not updating correctly.
Example Project
Paste this XAML into your MainWindow.
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding Path=List}" x:Key="myViewModel" />
<my:UpdateNotWorking x:Key="updateNotWorking" />
</Grid.Resources>
<DataGrid ItemsSource="{Binding Source={StaticResource myViewModel}}" Name="mylistbox"
SelectedItem="{Binding Source={StaticResource updateNotWorking}, Path=MyObjModel, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock TextWrapping="Wrap" FontWeight="Bold" Foreground="#FF50CEFF" FontSize="24" TextAlignment="Center"
Height="75">
<Run Text="{Binding Source={StaticResource myViewModel}, Path=text}" Foreground="#FF00E200" />
</TextBlock>
<TextBox Text="{Binding Source={StaticResource updateNotWorking}, Path=MyObjModel.text, UpdateSourceTrigger=PropertyChanged}"
Height="22" />
</Grid>
What it is now doing is setting updateNotWorking's MyObjModel property based on the DataGrid's SelectedValue, with UpdatePropertyTrigger set to PropertyChanged to see the changes immediately. No longer do we need to define updateNotWorking's property through the List's CurrentItem because it is not going to change just by selecting it with a DataGrid. You can keep it set, but it is not required as we are doing all the manual labor with the DataGrid SelectedValue.

Show only allowed data in WPF Listbox Databinding

I currently have a list box:
<ListBox HorizontalAlignment="Left"
ItemsSource="{Binding Data, ElementName=bookingDomainDataSource}"
Margin="158,134,0,45"
x:Name="bookingListBox"
Width="429"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay, ElementName=bookingComboBox}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=userId}"
Width="100" />
<TextBlock Text="{Binding Path=bookingName}"
Width="100" />
<TextBlock Text="{Binding Path=bookingDate}"
Width="100" />
<TextBlock Text="{Binding Path=showId}"
Width="100" />
<TextBlock Text="{Binding Path=paymentId}"
Width="100" />
<TextBlock Text="{Binding Path=ticketId}"
Width="100" />
<TextBlock Text="{Binding Path=ticketQuantity}"
Width="100" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I would like to only show rows from the itemssource that have a certain userId, how can I do this?
Thanks.
I think the best solution to this would be to filter the data source BEFORE you get to the front end.
You want to define a filter for your listview.
Uodate: sorry missed the silverlight tag. However CollectionViewSource should still be useful to you. Here a sample using the CollectionViewSource in Silverlight.

Categories