WPF - SuggestAppend for Textbox - wpf

I am looking for a textbox control that suggests words as the user types, similar to SuggestAppend for textboxes in winforms, except for WPF. I have looked around on the WPFToolkit and haven't really found anything that fits my needs.
Thanks.

Declare an enum AutoCompleteMode too with value(Append, None,SuggestAppend,Suggest)
public enum AutoCompleteMode
Create an custom UserControl with TextBox and ItemControls. Handle the KeyDown event of TextBox. Popup an custom List to show the suggestion list(ItemControls in here). Then Handle the selection of the ItemControls. Can custom the style of hte ItemControls's ItemTemplate. Apply the AutoCOmpleteMode in this UserControl and handle the Enum changed in the code behind.

WpfToolkit contains AutoCompleteBox that you can use for auto suggest feature.
You will have to define a collection for items to suggest (SuggestionItems) and set it as ItemsSource on AutoCompleteBox control.
<someNamespaceAlias:AutoCompleteBox
ItemsSource="{Binding SuggestionItems}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />

Related

How to enable wpf controls through data binding on a selected item from a combo box

I am looking for a way where a control can be enable when an item from a combo box is selected. Is there a simple way through data binding when a user selects an item from a combo box that it then enables another control to be used?
If you're using MVVM, you can bind the SelectedItem of the combobox to a property in your viewmodel.
Say this is your combobox:
<ComboBox ItemsSource="{Binding widgetlist}" SelectedItem="{Binding Path=selectedwidget, Mode=TwoWay}"></ComboBox>
And this is your control:
<DockPanel IsEnabled="{Binding controlenabled}">
...
</DockPanel>
Then in selectedwidget's setter, you can change the controlenabled property to False or True. Don't forget to notify that the controlenabled property changed (or if you want, make controlenabled a DependencyProperty.)
In summary, you've got 3 properties to bind to:
widgetlist, an ObservableCollection or some other collection that is the source for your combobox
selectedwidget, an item of that collection type that changes to whatever the combobox currently has selected
controlenabled, a bool that the other controls look at to decide if they are enabled/disabled.
Like many examples in MVVM, this way may require slightly more thought and code on the outset, but will be far more maintainable and scalable later. For example, say you want some more controls to also enable/disable themselves based on the same scenario. Piece of cake: add IsEnabled="{Binding controlenabled}"> to them.
Yes. You want to bind to IsEnabled in the target control which you want to dynamically enable or disable, and use a Value Converter to convert a matching string or item from the ComboBox to a true value for being enabled.

How to clear a TextBox in a User Control in WPF C#?

I have a user control that has a textbox in it, and i am using a clear button on my main form to clear information from the entire main window. i would like to clear the textbox in the user control once the clear button is clicked as well. i havent found an easy way to do this. i have tried referencing the control's name in c# followed by a "." however the name of the text box does not show up. any help would be appreciated!
WPF declares controls in a UserControl as private. To make your TextBox public you declare it with a FieldModifier as in:
<TextBox x:FieldModifier="Public" />
where x is the xaml namespace xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml". However the recommended way of clearing a TextBox is to bind it to a property and then clear the property.
You should not try to directly access controls within a UserControl from external classes or code. The simple mechanism would be to add a Clear() method to the UserControl which clears all relevant controls and information inside the UserControl.
The textbox could be bound to the DataContext of the UserControl. So a way of clearing it might be setting the property that is bound to the Text property of the TextBox to an empty string.

WPF - databinding label on listbox ismouseover

I'm still new to WPF, and I'm trying to do something that's beyond my knowledge at the moment.
I have a listbox databinded to the source collection, and a label. I'd like to bind the label's Content value to the listbox's item over which is mouse hovered.
Say I have DataTemplate binded to the class MenuItem:
<DataTemplate DataType="{x:Type local:MenuItem}" x:Key="MenuListTemplate">
Which has member Text. I want my Label to display Text from element which is mouse overed in list. I have the IsMouseOver trigger for my textbox, but have no idea how to bind Label.Content to it.
Any tips?
I don't think that binding can achieve your goal with ease. I think it's easier to do with routed events.
Subscribe to the MouseMove event at the ListBox level. Check if the source of the event is a ListBoxItem, and if it is use this item to update the label.

Wpf SelectedItem wont work for a Combobox in a ListView

I´ve got a problem with a Combobox in a ListView.
I´ve got a class called "Substrate". This class contains an object of a class called "SubstrateType". I want to show the objects of the class "Substrate" in a Listview. Each property of the "Substrate" is presented in the columns of the Listview. For the different "SubstrateType" I want to use a combobox.
In the XAML I tried it like this:
<ComboBox Name="CBType"
ItemsSource="{Binding ElementName=SettingsSubstrate, Path=TypeList}"
SelectedItem="{Binding Path=Substrate.SubstrateType}"
DisplayMemberPath="Description"/>
In the code-behind I got two ObservableCollections. One for all Substrates and one for all possible SubstrateTypes. The combobox displays all SubstrateTypes if you click on it. But the combobox has no selecteditem if you dont select one.
http://i44.tinypic.com/2eakxav.png
Thanks in advance.
I do not know your exact code, but if your ListView rows display objects of type Substrate, then your Binding Path for the SelectedItem should be just SubstrateType because the DataContext of a ListViewItem is already set to the Substrate object:
SelectedItem="{Binding Path=SubstrateType}"
Furthermore, you need to make sure that your SubstrateType instances are actually considered as equal. If the SubstrateType instance in your Substrate object is not exactly the same as the one from the TypeList property, it will not be selected. You can fix that by overriding the Equals(...) method and define your custom comparison for equality.
If this does not work, please provide more code, e.g. the surrounding XAML and the code of Substrate and the code-behind/ViewModel/whatever.

selected checkbox in WPF

I have a lot of check boxes in my WPF form. I want to get the selected checkbox value alone. In Winforms we can use foreach(checkbox ck in controls), but I cannot use like that in WPF Forms. How can i get the selected checkbox in WPF?
First of all, WPF is not just another replacement for WinForms, So the tricks in Winforms might be little different than WPF. WPF is all about DataBinding, so read about MVVM pattern which will really help you in WPF development.
Now coming to the way to go with MVVM approach fort this, Imagine your ViewModel class contains a collection of bool. Now the DataTemplate has CheckBox.IsChecked property bind to the boolean, So when you change the checkbox the collection will hold the changed booleans appropriately.
public List<bool> MyBoolCollection{get; set;}
<ItemsControl ItemsSource="{Binding MyBoolCollection}" ...>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
Can you data bind the IsChecked property for each CheckBox? Bind it to a member variable of the container class. At least this way you can iterate over the member variables to determine if any are checked.

Resources