I have a combobox in a wpf c# application.
What i am trying to do is the following.
I have a unselected combobox, as you look at it i can see an arrow to the right hand side and a space for text on the left. For the purpose of this question i'll refer to this text as 'Cell Text'.
When you select the button it appears with a list. I want this list to contain a number of robots my GUI/PC can connect to. When i select a robot, a message is sent to this robot trying to connect with it.
The 'Cell Text' i want to display the name of the currently connected Robot. There will be situations when a connection to a selected robot would'nt be possible, also a successful connection could take 5 seconds.
What i need to do is stop the selection automatically appearing in the 'Cell Text', is this possible?
Thanks
<ComboBox ItemsSource="{Binding MyRobotOptions}" Grid.Column="1" SelectedItem="{Binding SelectedRobot}" Margin="5"/>
For an inexperienced user (no offence), one of the easiest ways you can do this is to overlay a TextBlock over the 'Cell Text', as you call it:
<Grid>
<ComboBox ItemsSource="{Binding MyRobotOptions}" Grid.Column="1" SelectedItem="{
Binding SelectedRobot}" Margin="5" />
<TextBlock Text="{Binding YourSelectedRobotName}" Background="White"
Margin="0,0,24,0" />
</Grid>
I haven't been able to test this, so you might need to adjust the Margin property values to make it fit better, but it should hide the original text value.
A better solution I think is to use a separate indicator for connection state. For example a colored border around the Combobox that turns green when connected, red when disconnected. That way you don't have to break the paradigm of a ComboBox that everyone assumes: when you select something it immediately appears selected in the ComboBox.
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.
Is there a way to combine all the textboxes that this itemscontrol creates so I can select them all as in a single textbox or textblock? As of right now, I can only select the text in a single textblock at a time.
<ItemsControl Background="WhiteSmoke" ItemsSource="{Binding SelectedItemNotes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBox IsReadOnly="True" Background="Transparent" BorderThickness="0">
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1} - {3}">
<Binding Path="Timestamp" />
<Binding Path="UserName" />
<Binding Path="Notes" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
So as far as text selection interaction goes, you want the user to interact with the text in multiple text boxes it as if it were all in one big multi-line textbox: User clicks between the fourth and fifth characters in the first box, drags to just before the ninth character in the third box, and releases the left mouse button. The result is that everything after the fourth character in the first text box is selected, all of the second text box is selected, and the first eight characters of the third are selected.
There's nothing that'll do that for you. You'd be writing custom mouse interaction. I've done text selection with a mouse, though only in a single control, and it's finickier than you realize, but it's pretty awesome when it's finally working right.
So you'd set up some private state in your control class so the mouse events know that the user is engaged in this mega-selection interaction. Mousedown and drag in a textbox in the ItemsControl would launch that, mouseup in one would end it.
On mousemove, you'll have to set SelectionStart/SelectionEnd for each of the textboxes between the one you started at, and the one you're over. For the one you're over (if it's not the first one), you'll have to do custom code to find out which character the mouse is over, and programmatically set SelectionStart and SelectionEnd.
Don't forget that the user can drag-select "upward" from TextBox 2 to TextBox 0 you need to know which way the selection is going.
Once you know which textboxes are in your selection, write a property that enumerates those. Given that, once they have the appropriate text selected, it's easy to get the full selection text:
// In order of first in UI to last, regardless of "selection direction"
protected IEnumerable<TextBox> MegaSelectionTextBoxes {
get {
// Whatever -- might be a good idea to keep them
// in a List<TextBox> or ObservableCollection<TextBox>
}
}
// The easy part
public String MegaSelectionText {
get {
return
String.Join("\n",
MegaSelectionTextBoxes.Select(
tb=>tb.SelectedText));
}
}
Myself, I wouldn't count on getting that working really right in less than a week. Your definition of "right" may be a lot more permissive though.
But I would prefer an approach that works with XAML rather than trying to work around it.
If it were me, unless this feature was really critical for some reason (and sometimes they are), I'd make a case to sacrifice the partial selection in the first and last textboxes in the selection.
I'd look at using a multiselect ListBox rather than an ItemsControl, so I could let the user select items. Then I'd bind SelectedItems on the ListBox to an ObservableCollection on my viewmodel, and write a Command on my viewmodel that does whatever I want with the concatenated text of all the selected items.
Another option is use a DataGrid which multiple cell selection. That'll get you the interaction you want, though you may have to play some games to get it to look the way you want it to.
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.
I need to a collection of groupboxes varying on the user selection. for example; there will be 7 groupboxes, the user can enable however many they want and in what order they want. So i want the selected groupbox B to appear at the bottom of the previously selected groupbox A yet when A is unselected B moves up the form to where A was.
In my mind i want it to behave similar to HTML items.
This will be done in WPF, coding in C#.
You can stack these groupboxes in a stackpanel with orientation=vertical. You can then set the Visibility of the groupboxex to the users decision and wpf will make the rest for you "by magic".
Little sample here:
<StackPanel Orientation="Vertical">
<GroupBox x:Name="First" Visibility="Visible" Header="First">
<Label>First</Label>
</GroupBox>
<GroupBox x:Name="Second" Visibility="Collapsed" Header="Second">
<Label>Second</Label>
</GroupBox>
<GroupBox x:Name="Third" Visibility="Visible" Header="Third">
<Label>Third</Label>
</GroupBox>
</StackPanel>
Put your GroupBoxes in a collection of some kind and databind that collection to a cusomised ListView. Whenever the selected state of a GroupBox changes update the view of that ListView to sort them based on your requirements. Unfortunately I am not good enough to provide a working sample in the time I have, sry.
I am trying to create some form of updating area in wpf. It needs to be up datable as it will be connected to a live stream of text that will constantly need to be displayed.
The idea is that I will have a stream of data which will comprise of a UserName and Text, this will come in a random times and need to be displayed:
User:Test :: Test:TextData
User:NextTest :: Test:TestData
and so on each item on a new line, so the object needs to be up datable in a scrolling format so the new item will be added to the bottom.
Currently I am using:
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<TextBox Margin="5" TextWrapping="Wrap" AcceptsReturn="True" IsReadOnly="True" x:Name="LogDetails"></TextBox>
</StackPanel>
</ScrollViewer>
However this does not really show the data very well, the data is just string based, does anyone know of a better solution?
Thanks
The question is a little bit vague, but here a try:
As an option, why not using TextBlock instead of TextBox, if the text changes automatically?
Caveat: If you want to use the TextBox, don't forget to set the UndoLimit to 0 . Otherwise, you will have a lot of memory consumption, if you change the TextBox contents continously.
<TextBox UndoLimit="0" .../>
If your text is a concattenation of multiple string-elements, create a layout with a grid and use multiple TextBlocks to shown the data more nicely? Maybe there is also some data you can visualize as symbols?
If it is a log, maybe you want to fill a list with strings and set this list as the ItemsSource of an ItemsControl? Through the ItemTemplate-property you can then specify the layout of each item? Use an ObservableCollection<string>, then you only have to add the strings to the collection and the ItemsControl will refresh automatically. You can use ItemsControl, ListBox, ListView for such a log.