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".
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 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}" />
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 having a problem. I have two combobox in WPF first:
<ComboBox commands:PropertyChangeBehavior.Command="{Binding GetCurrentsModuleCommand}" SelectedIndex="0">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="True">All</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=AxmModulesCombo}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</StackPanel>
Lets call it Source and it is binded to command:
public ICommand GetCurrentsModuleCommand => new Command(module =>
{
SelectedAxmModule = module.ToString();
Stuff(module);
});
And this combo box has no SelectItem property (well it don't needs any as parameters are only pass to method).
And a Target CombxBox
<ComboBox ItemsSource="{Binding AxmModules}"
SelectedItem="{Binding SelectedAxmModule, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="{Binding SelectedAxmModules}"
IsSynchronizedWithCurrentItem="True"/>
Witch SelectedItem property is binded to:
private string selectedAxmModule;
public string SelectedAxmModule
{
get => selectedAxmModule;
set
{
selectedAxmModule = value;
OnPropertyChanged();
}
}
Now those combo boxes are similar in values, now I want to do if I click value on source as you see in source command i want to select same value from source (I handled values from source and not in target in code so that is irrelevant).
I tried adding Update property and SelectedValuePath but no good value in target combo box is still empty after selecting value from source, and OnPropertyChange works as inteded. Is there a way to achieve this?
If AxmModules is an IEnumerable<byte>, SelectedAxmModule should be a byte property. You cannot set a string property to a byte value. The types must match.
And SelectedValuePath is superfluous when using SelectedItem.
Since you have
IsSynchronizedWithCurrentItem="True"/>
Then the selecteditem should be the current. You should work with the current item of the collectionview rather than binding selecteditem.
You can read more, probably more than you want to read, about collectionview and this sort of stuff here:
https://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx
Also.
See this
SelectedValuePath="{Binding SelectedAxmModules}"
The selectedvaluepath is a string not a binding. It should be the name of whichever field you want to use.
If, for whatever reason, you want to bind to the current item in a collection then you can use the / notation.
https://social.technet.microsoft.com/wiki/contents/articles/29859.wpf-tips-bind-to-current-item-of-collection.aspx
I'm not sure how this is going to fit with a composite collection though. I prefer a separate control to indicate selection of "all" and hence wouldn't use a composite collection in this way. You might want to consider that approach yourself.
In a project I am showing two ComboBoxes lets say ComboBox1 and ComboBox2. I am binding both the ComboBox with a KeyValue pair Dictionary lets say dictionary1 as given below.
ComboBox1.ItemsSource = dictionary1 ;
ComboBox1.SelectedItem = ComboBox1.Items[0];
//Setting the Item Source of Patient Name Combo Box.
ComboBox2.ItemsSource = dictionary1 ;
ComboBox2.SelectedItem = ComboBox2.Items[0];
and in the XAML part, I am showing Key in the CombBox1 and Value in the ComboBox2 of the Dictionary as mentioned below:-
<ComboBox
x:Name ="ComboBox1"
DisplayMemberPath ="Key"
SelectedValue ="{Binding Source=ComboBox2, Path=DisplayMemberPath, Mode=TwoWay}"/>
<ComboBox
x:Name ="ComboBox2"
DisplayMemberPath ="Value"
SelectionChanged ="ComboBox2_SelectionChanged"
/>
Objective:-
If I change the selection in ComboBox1 then it should affect the corresponding value of the ComboBox2.SelectedItem and also if I change the selection in ComboBox2 then it should affect the corresponding key value in the ComboBox1.SelectedItem.
Can anybody tell me where is the mistake in my above code or please help me in accomplish the above said objective. Thanks in advance.
This should work
<ComboBox
x:Name ="ComboBox1"
DisplayMemberPath ="Key"
SelectedItem ="{Binding ElementName=ComboBox2, Path=SelectedItem, Mode=TwoWay}"/>
<ComboBox
x:Name ="ComboBox2"
DisplayMemberPath ="Value"
/>
I can see at least two problems:
Needs to be ElementName instead of Source
Should be Path instead of DisplayMemberPath
I think this should work:
<ComboBox
x:Name ="ComboBox1"
DisplayMemberPath ="Key"
SelectedValue ="{Binding ElementName=ComboBox2, Path=SelectedValue}"/>
<ComboBox
x:Name ="ComboBox2"
DisplayMemberPath ="Value" />