I try to use a CheckComboBox control from the Xceed library.
When I select an item, the control displays system.windows.controls.comboxitem:"value" instead of just value
Is there a way to display only the value of the selected item without its type?
<xctk:CheckComboBox>
<ComboBoxItem Content="SA" />
<ComboBoxItem Content="NA" />
</xctk:CheckComboBox>
In this particular case it can be solved by adding the DisplayMemberPath="Content"
<xctk:CheckComboBox DisplayMemberPath="Content">
<ComboBoxItem Content="SA"/>
<ComboBoxItem Content="NA"/>
</xctk:CheckComboBox>
But whether it is a designed feature or just a serendipitous behaviour, I am not sure.
So overall it would be better in the future to read on WPF binding, and use datasources and bindings to make the code similar to what is on the documentation page.
<xctk:CheckComboBox
DisplayMemberPath="Color"
ValueMemberPath="Level"
SelectedValue="{Binding SelectedValue}"
SelectedItems="{Binding SelectedItems}" />
Related
I want to make a ComboBox with 0-N Separators
I was able to do this with CompositeCollection like so
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection ={Binding mysrc1}/>
<ComboBoxItem IsEnabled = "False">
<Separator Background = "White" Height=10/>
</ComboBoxItem>
<CollectionContainer Collection ={Binding mysrc2}/>
<ComboBoxItem IsEnabled = "False">
<Separator Background = "White" Height=10/>
</ComboBoxItem>
</CompositeCollection>
</ComboBox.ItemsSource>
But this is kind of clunky and with a lot of code duplication but in .View and in .ViewModel
I was thinking about using custom control that will Inherit from ComboBox and use DependencyProperty.RegisterAttached for the location of the Separator(s)
but couldn't find a way to do so
P.S
i prefer not use any code behind and not use <Setter Property="Template"> because i have base style and template that a lot of ComboBoxs in my program user
As you are using databinding, the items are grouped, so I would using grouping.
See this answer: Grouping items in a ComboBox (solved)
If you do not want to use grouping, then you could use this method: Insert ComboBox Item Separator Which is Filled Through Data Binding
I have a combobox in a WPF XAML window. I want to have an IF statement in my .cs where I can then assign a method to it when the user has that item selected.
Here is the XAML bit:
<ComboBox x:Name="comboBoxThickness" HorizontalAlignment="Left"
Margin="469,380,0,0" VerticalAlignment="Top" Width="155" IsEditable="True"
MaxWidth="150" Text="Select Plate Thickness">
<ComboBoxItem x:Name="Combo8mm" Content="8mm" />
<ComboBoxItem x:Name="Combo12_5mm" Content="12.5mm" />
</ComboBox>
Here is the .cs part (with an attempt made):
private void WeightCal()
{
if (Combo8mm.Selected){
}
}
Based on your question, I have added code snippet assuming that you wanted to do something on combo-box selected changed event.
Case 1 :-
I have done little modification to your xaml and code-behind.
Added a SelectedChanged event to combobox as below.
<ComboBox x:Name="comboBoxThickness" HorizontalAlignment="Left" VerticalAlignment="Top" Width="155" IsEditable="True"
MaxWidth="150" Text="Select Plate Thickness" SelectionChanged="comboBoxThickness_SelectionChanged">
<ComboBoxItem x:Name="Combo8mm" Content="8mm" />
<ComboBoxItem x:Name="Combo12_5mm" Content="12.5mm" />
</ComboBox>
Then in the code behind in the event handler, you can check which Combobox item is selected like below,
private void comboBoxThickness_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = comboBoxThickness.SelectedItem as ComboBoxItem;
if(selectedItem.Content.ToString() == "8mm")
{
// Write your logic here
}
}
You can multiple if..else conditions to check and do necessary actions in each if condition as per your requirements.
Note:- Even though the approach you have followed was not recommended. Always rely on MVVM which helps you in long run.
And in the above code I am just trying to compare the selected combo-box item content with string. Assuming it is always a string in your case. You have to check and test the code in your project.
Case 2:-
In case if you want to get the selected item in some method, you can directly use this below statement instead of checking that each combo-box item is selected or not which you have written in your code.
var selectedItem = comboBoxThickness.SelectedItem as ComboBoxItem;
if(selectedItem.Content.ToString() == "8mm")
{
// Write your logic here
}
If there is something else that you are looking for, then let us know.
I am trying to display values of all items present in a comboBox. The items are displaying as list after running the emulator.In addition, the combobox is not clickable to select an item.
<ComboBox FontWeight="Bold" x:Name="FStyle" HorizontalAlignment="Left" Margin="192,435,0,0" VerticalAlignment="Top" Height="27" Width="154" SelectionChanged="FStyle_SelectionChanged">
<ComboBoxItem Content="Normal"/>
<ComboBoxItem Content="Italic" />
</ComboBox>
What could be the problem.
Maybe it's stupid but ...
Remove that killing margin and set Height to auto, also as a general tip for future never hardcode Height and Width, but try to position controls in containers controls.
Good luck.
I have the following ComboBox:
<ComboBox x:Name="splitInput_ComboBox" SelectionChanged="spltComboBox_SelectionChanged">
<ComboBoxItem Tag="0" IsSelected="True">None</ComboBoxItem>
<ComboBoxItem Tag="2">2</ComboBoxItem>
<ComboBoxItem Tag="3">3</ComboBoxItem>
</ComboBox>
How can I assign the user selection to an int or double variable in the code behind? Is data binding the only way to accomplish this?
In your spltComboBox_SelectionChanged event handler you can get selected item or selected value using these properties:
SelectedItem
SelectedValue
like:
splitInput_ComboBox.SelectedValue;
Note: I recommend you not to use underscore while naming your controls as you did "splitInput_ComboBox".
I recently upgraded my Silverlight app from 3 to 4. After a few hours of bashing my head against the wall, trying to resolve an issue, I've narrowed down the problem to this:
I have a user control, with a ComboBox inside it. The ComboBox has a single ComboBoxItem child. The user control exposes a get accessors that returns the ComboBox's Items object, allowing me to add additional ComboBoxItems via xaml.
This all worked fine in Silverlight 3, however it's not working in Silverlight 4.
As code:
//XAML
<UserControl ... >
<ComboBox Name="myComboBox">
<ComboBoxItem Content="Select an Item" />
</ComboBox>
<!-- All my other stuff -->
</UserControl>
//Code behind
public ItemCollection ListItems
{
get
{
return myComboBox.Items;
}
}
//Implementation of User-Control
<CustomControl:UserControl ... >
<CustomControl:UserControl.ListItems>
<ComboBoxItem Content="Item 1" />
<ComboBoxItem Content="Item 2" />
<ComboBoxItem Content="Item 3" />
</CustomControl:UserControl.ListItems>
</CustomControl:UserControl>
As I mentioned, this all worked fine in Silverlight 3, but doesn't work in Silverlight 4.
The workaround, it seems, is to remove that single ComboBoxItem that's inside my user-control, but I'm hoping to avoid, as I want it as the default item.
Any help would be much appreciated!
The XAML parser was re-written for Silverlight 4 to make it more consistent with WPF. I am pretty certain the behavior you are expecting was a bug in SL3 and I don't think it would have worked that way in WPF though I've never actually tried.
You may be able to get the old mode back by enabling quirks mode but I wouldn't recommend it. Instead, what I would do is create a ControlTemplate for the combo box to display the "select an item" text when there is nothing selected. Having that be an actual item in the combo box really just a hack that we've always been forced to do with technologies such as Windows Forms and HTML but in Silverlight it seems like having SelectedItem be null is more appropriate.