I have on combobox with products, one textbox. In text property of textbox I want to bind selected product's price from combobox. To that moment it can be made for example like this:
<TextBox Name="txtSalePrice" InputScope="Digits" Text="{Binding ElementName=cmbProducts, Path=SelectedItem.Price}" />
<ComboBox x:Name="cmbProducts" ItemsSource="{Binding ListProducts}" DisplayMemberPath="FullName">
Now I want to bind value of textbox to property of some object let's say it's
new sale object 'NewSale' with property 'price', but without change price in selectedProduct.
I have problem with achieve this.
Related
I have problem with binding 3 things i xaml.
I want to made something like this:
I choose product from combobox.
Default price from this products is setting in textbox.
Now I want for example change this price and bind it to my object 'newSale'
<TextBox Name="txtSalePrice" InputScope="Digits" Text="{Binding ElementName=cmbProducts, Path=SelectedItem.Price}" \>
<ComboBox x:Name="cmbProducts" ItemsSource="{Binding ListProducts}" DisplayMemberPath="FullName" \>
I want to bind value of textbox to property of object 'NewSale' with property 'price', but without change price in selectedProduct.
I have problem with achieve this.
The easiest way I see is to use another property (Price for example) bound to your TextBox. In you viewmodel, whenever the SelectedItem of your ComboBox changes, set it into Price. And whenever you want to display something else, set manually the value into Price.
Imagine your working on a UI for an Wedding Planner app. You'll have a list of guests and you want to display their name on the screen next to a combobox containing values of 'Will Attend' / 'Maybe' / 'No'.
I've tried to something just that....I have a collection of items inside a view model (the guests). For each of those items I want to display a Label and a ComboBox. Each ComboBox has the same values in the drop down (the possible responses).
I've created an ItemTemplate that contains a label and a combobox. I bind it to my collection of guests and it works as expected. I'm using ancestor binding so that the ComboBox's ItemsSource is bound to the list of possible responses. That works great.
What I'm struggling with is how to bind the SelectedItem to get the values the user selects? I want to have a collection of selected values on the ViewModel somehow, but I'm having a lot of trouble finding the correct words to describe this / search for it.
Can anyone help me? Am I going about this the wrong way?
You may create an enum for the attendance state and add an Attendance property to your Guest class:
public enum Attendance
{
Yes,
No,
Maybe
}
public class Guest
{
...
public Attendance Attendance { get; set; } // raise PropertyChanged event if necessary
}
Now you could set the Tag property of the ComboBox items to the appropriate enum value and bind the SelectedValue property:
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding Attendance}" SelectedValuePath="Tag">
<TextBlock Tag="Yes">Will Attend</TextBlock>
<TextBlock Tag="No">Won't Attend</TextBlock>
<TextBlock Tag="Maybe">May Attend</TextBlock>
</ComboBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
I have a combo box and button as such
<ComboBox ItemsSource="{Binding MessageTypesList}"
DisplayMemberPath="MessageType"
SelectedValue="MessageType" />
<Button Content="Search"
Command="{Binding Path=SearchMessageTypes}"
x:Name="SearchMessageTypeButton"/>
The MessageTypesList list is generated from a SQL query and once the Message Type is selected from the list the Search button needs to pass the selected value to a string property in my ViewMainModel.
When I debug the application the value passed to the MessageType property is always NULL. I have this working for a similar date time searchs but cant see how to pass the MessageType value in my XAML to the MessageType propery form binding generated lists.
You should bind the SelectedValue property to a property in your viewmodel.
Create a property in your viewmodel:
public MessageType SelectedType {get;set;}
Bind selectedItem to this property in XAML:
<ComboBox ItemsSource="{Binding MessageTypesList}" SelectedItem="{Binding SelectedType, Mode=TwoWay" />
I'm using a combobox to show categories a user can choose.
All those items have the id as the SelectedValuePath so I can easly get those when the user makes a selection.
<ComboBox Grid.Column ="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Name="cboCategorieen" ItemsSource="{Binding}" DisplayMemberPath="naam" SelectedValuePath="id" />
But how can I change the selection of the combobox when I have the id of the chosen selection (SelectedValuePath)?
This code doesn't do anything and just keeps selecting the first one.
cboCategorieen.SelectedValuePath = Convert.ToString(artikelWijzigen.categorie);
To conclude:
How can I change the selection of the combobox to the one matching the id?
I would suggest you to follow MVVM, Still the answer for your question would be Say if you have a ItemsSource like this,
ObservableCollection<YourComboBoxClass> wsWebshopMRentals;
You can set the selectedItem of combobox like this,
cboCategorieen.SelectedValue = wsWebshopMRentals.FirstOrDefault(x => x.Id == YourID).naam;
I have a combobox, which is bound to a dictionary of
<int, MyObject>
by the following code:
this.comboBox.ItemsSource= dictionary;
MyObject has some public properties, such as Name (string) and Selected (boolean).
In my Xaml I have an ItemTemplate on comboBox:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Key, Mode=OneWay}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
When I run the app, I get the combobox filled with [1, MyObject] values.
What I eventually want to do is have a checkbox and a textblock in the ItemTemplate, which will be bound to MyObject.Selected and MyObject.Name. At the moment, I can't even get the datatemplate to display the dictionaty Key value.
Any ideas why the itemtemplate is being ignored? I've tried the full temeplate (stack planel containing a combobox and textblock), that showed the same values at runtime.
cheers.