Question:
What is the simpliest way for pre-defined items in a Listbox to be populated to a TextBox (Preferably single selection) In a Windows 7 Phone Enviroment?
Example: I select "Bob" from the list box and then "Bob" is then displayed in the text box instantly so the user doesn't need to type in a Username, and can instead use a pre-made username from the list of Usernames.
Problems:
I've tried looking for these little things and found no solutions.
The solutions I did find are incredibly vague.
Many videos on YouTube and elsewhere haven't really covered this for Windows 7
I fear Windows 8 is different.
Right now, my code fails to populate the Textbox with the Listbox Selection.
Current Code:
<TextBox HorizontalAlignment="Left" Margin="75,175,0,488" Name="textBox1"
Text="{Binding ElementName=listBox1, Path=SelectedValue}" Width="298" FontSize="20"
BorderBrush="Transparent" OpacityMask="#6E030303" Foreground="#FFEBC285" Background="Black"
FontFamily="Trebuchet MS" MaxLength="7">
In your XAML you can do something like this
<ListBox name="lb"/>
<TextBox Text="{Binding ElementName=lb, Path=SelectedValue}"/>
What this does is it databinds the TextBox.Text property to the ListBox.SelectedValue property`. The Textbox should populate when you select new items in the listbox.
Related
I'm trying to highlight part of text in a textblock from a listbox datatemplate which in turn is bounded to a property of a custom class by using a textbox to search the list for input text. But the problem is that only part of the items are highlighting (most of the ones visible) but when i maximize the window and try to input another character then suddenly all of them gets highlighted my guess where the problem might be is in this piece of code:
ListBoxItem listboxItemFound= (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
Since this method is returning a null when the items are not visible but the items are currently in the listbox. Somehow I guess the items listboxItem instances are not yet created until you scroll down or maximize to view more items.
XAML DataTemplate:
<DataTemplate>
<Grid Name="gridOfListbox" Height="25" Margin="0,2">
<DockPanel Name="dockpanelWithTxtBlock">
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Text="" /><Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>
</DockPanel>
</Grid>
</DataTemplate>
If more code is needed just let me know.
Any help would be greatly appreciated!!
Also if there is any other better way of finding the listboxItem bounded to the custom Item just let me know. Thank you very much!
[Pic of problem] http://i.stack.imgur.com/HViag.png
One way to fix this is to set VirtualizingStackPanel.IsVirtualizing to false for your ListBox. This will cause all of the items to be created right away. The downside to this is if your ListBox has many items, your program will use more memory (since more items will be created), and could potentially run slower depending on the number of items.
A better solution to consider would be to have multiple DataTemplates for this - one without the highlight, and one with. You can set a DataTemplateSelector for your ListBox (using the ItemTemplateSelector property). The selector can choose which template to use based on if the item matches the search term or not.
The tricky part would be writing the template with the highlighted text. You could probably achieve that by having properties on the object the ListBoxItem is bound to for the text before the highlighted text, the highlighted text, and then the remaining text.
I have a combo box in a wpf c# application. In the xaml i am trying to do the following.
The ItemsSource comes from one variable.
SelectedItem sets a value on another variable
But i want the text displayed to come from a new variable.
How do i stop making the selected itemssource appear as the main text?
<ComboBox x:Name="ComboPlay" FontSize="14" MinHeight="20" Margin="0,2,4,4" Grid.Row="1" Grid.Column="3" MinWidth="160"
ItemsSource="{Binding ComboBoxList}"
SelectedItem="{Binding OutputChannel.Value, Converter={StaticResource ResourceKey=ValueToStringConverter}}" Grid.ColumnSpan="1"
IsEnabled="{Binding IsDriveChoiceEnabled}"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
If you mean changing the display of the item currently selected (the portion of the control shown when the dropdown is closed), take a look at Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?
Honestly, a quick search dug up that one and many similar ones. Probably the simplest way, like the linked answer, is to figure out if your item is wrapped in a ComboBoxItem and display it differently then. Or you could re-template the ComboBox. Or you could derive from it (and re-template it) and provide a separate dependency property for the template of the selected item, if you expect to reuse it in different contexts. The sky's the limit.
In an application of mine, I'm using the WPF autocomplete box from the wpf toolkit. I'm implementing it via the MVVM pattern. The binding works fine, but I have a small problem when trying to clear the content of the autocompletebox. Setting the bound property in the viewmodel to null, clears the text only partially (all text entered via the keyboard is not cleared - i.e. if I enter CH when fetching all the cities and select Chicago and the set the bound property to null, the CH is not being cleared , the rest ICAGO is.)
The XAML looks like this:
<my:AutoCompleteBox Grid.Row="0"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="0,6,0,0"
Name="acTown"
SelectedItem="{Binding NewTown, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ValueMemberBinding="{Binding Converter={StaticResource TownConverter}}"
Populating="Populating"
VerticalAlignment="Top"
Height="Auto"
</my:AutoCompleteBox>
The method in the viewmodel to clear the box is:
public void ClearTown()
{
NewTown = null;
OnPropertyChanged("NewTown");
}
I can't figure out what's wrong with the code, or is this just a bug in the autocompletebox?
After extensive research, I found this article: How do you clear the Silverlight AutoCompleteBox SearchText using MVVM, but it does not offer a solution. There seems to be a SearchText property on the AutoCompleteBox that is readonly and cannot have a setter
Finally got it. If anyone's interested, the solution is to simply change NewTown = null to NewTown = new NewTown() in the ClearTown function.
I'm using a ComboBox defined by:
<ComboBox Grid.Column="2" Height="29" HorizontalAlignment="Left" Margin="137,192,0,0" Name="componentsComboBox" VerticalAlignment="Top" Width="224"
IsEditable="True"
TextSearch.TextPath="Name">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
..to display a list of objects by their "Name" properties. I'm observing the following behaviors:
Click an item in the dropdown, and componentsComboBox.SelectedValue (and .SelectedItem) correspond to the clicked item. OK!
Start typing the name of an item, autocomplete fills in as you type, .SelectedValue (and .SelectedItem) correspond to the autocompleted item. GREAT!
Start typing the name of an item, autocomplete fills in as you type, hit delete to truncate to only what you have actually typed, .SelectedValue and .SelectedItem STILL correspond to the autocompleted item. NO! BAD WPF! BAD!
Similar behavior to 3 if you delete characters from the end of the textbox portion
In essence, if I have a List containing two objects defined by, e.g.,
{ new Component() { Name = "COMPONENT1"},
new Component() { Name = "COMPONENT2"} }
I want the values:
COMPONENT1
COMPONENT2
to appear in the drop-down portion, and if the user enters "COMP" I would like to recognize that they have entered a new value, but as it stands right the control makes it look like they selected COMPONENT1.
What am I missing here?
You can do this in WPF easily by setting the IsEditable and IsReadOnly properties to True & False respectively.
Ref.: http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox.iseditable.aspx
e.g. How can I allow users to edit text in a ComboBox in WPF?
<Window x:Class="LearnWPF.EditableComboBox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LearnWPF.EditableComboBox" Height="300" Width="300"
>
<Window.Resources>
<XmlDataProvider x:Key="items" XPath="//item">
<x:XData>
<items xmlns="">
<item>01</item>
<item>02</item>
<item>03</item>
</items>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<ComboBox IsEditable="True" DataContext="{StaticResource items}"
ItemsSource="{Binding}"/>
</Grid>
</Window>
I experienced this behavior today and agree that on the surface, it appears to be a bug. Digging a little deeper though reveals a bit of a gray area.
What I think is going on here is that the control is running a prefix matching algorithm every time the text box is updated, regardless of the keystrokes that cause the text box to be updated. So when you delete the auto-completed part of the text, the prefix matching algorithm still matches an item in the control's ItemsSource; the control isn't attempting to deduce that the end-user just deleted the auto-completed portion, so the algo shouldn't run.
Re: your comment to akjoshi's response, when you delete the "2", leaving just the "0" in the combo box's text box, the prefix matching algorithm correctly matches the "01" item.
The work-around I used was to only bind to the Text property of the ComboBox and build all view model logic off of that. SelectedItem, SelectedIndex, and SelectedValue are not bound at all.
Backgorund
I am currently writing a program that allows a user to select a manufacture from a combo box. The combo box is created in wpf using the following wpf code segment:
<ComboBox Height="23" Margin="40.422,128.423,229.908,0" Name="itemProductManufacture" ToolTip="Click to open drop down menu" VerticalAlignment="Top" Text="Select A Manufacture" SelectionChanged="itemProductManufacture_SelectionChanged" DropDownOpened="itemProductManufacture_DropDownOpened">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ManufactureId}" Width="0"/>
<Image Name="itemManufactureImage" Source="{Binding ManufactureImage}" Height="15" Width="70" Stretch="Uniform"/>
<TextBlock Text="{Binding ManufactureName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The data is provided form a database and each entry has a Image, a name and an Id (intentionally not shown)
Problem
I am trying to code the behaviour of the combo box so when it is open the image height is 50 and when it is closed it is 15 this is so the image is larger when it is first displayed and then smaller once selected so it doesn't take up too much space on the form.
I have tried editing the image propities using code but am unable to accsess it using its name or any other children of the combo box.
Thanks
Jonathan
As you are using data template you won't be able to access the directly by its name.
Try something like this -
Image image = this.itemProductManufacture.ItemTemplate.FindName("itemManufactureImage", this) as Image;
One thing I am not clear is whether you want to change image size for all the items or the selected one? If you need to access the image for a particulat item in combobox you may have to use the ItemContainerGenerator.ContainerFromItem, as explained in following posts -
WPF - ItemsControl - How do I get find my "CheckBox" item that is in the ItemTemplate?
http://www.sitechno.com/Blog/HowToUseAttachedPropertiesAsAnExtensionMechanismForACheckedListbox.aspx
look at this, To know the various ways of finding controls - How can I find WPF controls by name or type?
You can edit image properties from code using binding. Or you can use triggers in Datatemplate. When comboboxitems checked properties change, you can change height property of corresponding image
Try this:
<Image Height = "{Binding Path=IsDropDownOpen,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ComboBox}},
Converter={StaticResource myBoolToHeightConverter}}" />
An example for Converter here