Programatically Select a combobox item using the SelectedValue WPF - wpf

I have a combo box which is loaded through a Datasource(Datatable). On a scenario I want the combo box to load with the desired value which I would be passing as combobox1.SelectedValue = custId (Say it's a customer details). custID is set as SelectedValuePath in XAML. When I set that, I am getting a null exception. Anything I am missing?
My XAML:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="332,42,0,0" Name="cmbCustomerName" VerticalAlignment="Top" Width="240" IsEditable="True" DisplayMemberPath="customername" SelectedValuePath="custid" ItemsPanel="{StaticResource cust}" SelectionChanged="cmbCustomerName_SelectionChanged" AllowDrop="True" FontWeight="Normal" Text="--Select a Customer Name--" IsSynchronizedWithCurrentItem="True" />
UPDATE:
C# code:
public customer(int custid)
{
InitializeComponent();
cmbcustomer.SelectedValue= custid.ToString();
}
private void cmbCustomerName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
cmbcustid.SelectedValue= cmbcustomer.SelectedValue;
}

The selectedValue would not work in this scenario,you should use selectedItem instead,for example:
combobox1.SelectedItem = "custId";
or
combobox1.Text = "custId";
or
combobox1.SelectedIndex = combobox1.Items.IndexOf("custId");
or
combobox1.SelectedIndex = combobox1.FindStringExact("custId")

I will suggest you a method
This is hackish. This is bad coding format.
// Remove the handler
cmbcustomer.SelectionChanged -= cmbCustomerName_SelectionChanged;
// Make a selection...
cmbcustomer.SelectedIndex = combobox1.Items.IndexOf(custid.ToString()); //<-- do not want to raise
// SelectionChanged event programmatically here
// Add the handler again.
cmbcustomer.SelectionChanged += cmbCustomerName_SelectionChanged;
As a temporary solution you can try this out

Wait to access any properties of the ComboBoxes until the they have been loaded:
private void cmbCustomerName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(cmbcustid != null && cmbcustid.IsLoaded && cmbcustomer != null && cmbcustomer.IsLoaded)
cmbcustid.SelectedValue = cmbcustomer.SelectedValue;
}

I got the issue resolved by assigning the value in the combox_loaded event, as the combobox was just initialized in the constructor, but it was not loaded from the datasource.

Related

WPF MVVM Light - Binding of SelectedItem not changing

Quite a few posts around this area, but none are helping me... here's the scenario: I've got two "season" drop downs to simulate a range. If you pick a season in the begin range one, the viewmodele automatically sets the property bound to the end range to the same season (so it defaults to a single year and not a range. Here's what the XAML looks like (removed lot of the formatting attibutes for readability):
<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}"
SelectedItem="{Binding SelectedBeginRangeSeason, Mode=TwoWay}"
ItemTemplate="{DynamicResource SeasonItemShortFormat}" />
<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}"
SelectedItem="{Binding SelectedEndRangeSeason, Mode=TwoWay}"
ItemTemplate="{DynamicResource SeasonItemShortFormat}" />
The properties in the view model look like this:
private Season _selectedBeginRangeSeason;
private const string SelectedBeginRangeSeasonPropertyName = "SelectedBeginRangeSeason";
public Season SelectedBeginRangeSeason {
get { return _selectedBeginRangeSeason; }
set {
if (_selectedBeginRangeSeason != value) {
var oldValue = _selectedBeginRangeSeason;
_selectedBeginRangeSeason = value;
RaisePropertyChanged<Season>(SelectedBeginRangeSeasonPropertyName, oldValue, value, true);
}
}
}
private Season _selectedEndRangeSeason;
private const string SelectedEndRangeSeasonPropertyName = "SelectedEndRangeSeason";
public Season SelectedEndRangeSeason {
get { return _selectedEndRangeSeason; }
set {
if (_selectedEndRangeSeason != value) {
Debug.WriteLine("Updating property SelectedEndRangeSeason...");
var oldValue = _selectedEndRangeSeason;
_selectedEndRangeSeason = value;
Debug.WriteLine("Broadcasting PropertyChanged event for property SelectedEndRangeSeason...");
RaisePropertyChanged<Season>(SelectedEndRangeSeasonPropertyName, oldValue, value, true);
}
}
}
private void UpdateSelectedSeasonSelectors() {
// if the end range isn't selected...
if (_selectedEndRangeSeason == null) {
// automatically select the begin for the end range
SelectedEndRangeSeason = _selectedBeginRangeSeason;
}
}
I've verified the end property is being changed both with the debug statements and with unit tests, but the UI isn't changing when I select it... can't figure out what's going on and have looked at this so many different ways...
Did you get the SelectedSeason from the AvailableSeasons collection? If not, did you implement anything special to compare Seasons?
For example, suppose you have
<ComboBox ItemsSource="{Binding AvailableSeasons}"
SelectedItem="{Binding SelectedSeason}" />
If SelectedSeason = new Season(); the SelectedItem binding won't work because new Season(); does not exist in AvailableSeasons.
You'll need to set SelectedSeason = AvailableSeasons[x] for SelectedItem to work because that makes the two items exactly the same. Or you can implement some custom method to compare the two seasons to see if they're the same. Usually I just overwrite the ToString() method of the class being compared.
Try to fire an event from the ViewModel to notify the UI to refresh the calendar.

How to add a new line on TAB key in WPF dataGrid

I want to add a new line in my datagrid when I press 'TAB' key on last cell of the datagrid.
I am using MVVM pattern to do this. I have came with a solution, I assinged Tab key to the Input binding of the datagrid:
<DataGrid.InputBindings>
<KeyBinding Command="{Binding Path=InsertNewLineCommand}" Key="Tab"></KeyBinding>
</DataGrid.InputBindings>
And added following code to InsertNewLineCommand:
private void ExecuteInsertNewLineCommand()
{
//Checked is SelectedCell[0] at last cell of the datagrid
{
InsertNewLine();
}
}
But the problem is ON ADDING KEYBINDING='TAB' MY NORMAL TAB FEATURE ON THE GRID DISABLES (MOVING TO NEXT CELL AND SO...)
Just determine if you are on the last column then execute your command.
I am using PreviewKeyDown, so I could test the logic, but you can put that in your executeCommand method. Anyway, this should get you started:
<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown" SelectionUnit="Cell" ....
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!Keyboard.IsKeyDown(Key.Tab)) return;
var dataGrid = (DataGrid) sender;
var current = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn);
var last = dataGrid.Columns.Count - 1;
if (current == last)
ExecuteInsertNewLineCommand();
}

How to store a hidden value in a WPF combo box for use in SelectionChanged method

I have a ComboBox:
<ComboBox Name="Gen2Fis" ItemsSource="{Binding Path=Table}" SelectionChanged="Gen2Fis_SelectionChanged" DisplayMemberPath="LongName">
The query used to fill this combobox is:
Select ShortName, LongName from Table;
Based on the item selected from this list I want call another method with the selected item, but I need to use the ShortName (that isn't displayed) instead of the LongName (which is).
How would I go about doing this? Can I somehow hide the shortname in the list?
My method for loading the combo box:
public void LoadFINamesIntoList(string mainDB)
{
XiphosStr.ConnectString = mainDB;
dbConnection = new MyDatabaseConnection(XiphosStr.ConnectString);
DataSet ds = dbConnection.ExecuteQuery(Queries.getFIs);
Gen2Fis.DataContext = ds.Tables[0].DefaultView;
}
My method for the selection change:
private void Gen2Fis_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string gen2fi = (XiphosDB2.SelectedItem as ComboBoxItem).Content.ToString();
Gen2Str.ConnectString = gen2fi;
DisplayGen2Users();
}
gen2fi will equal the long name, but I need the short name.
Thanks for any help.
Jason
use SelectedValuePath from combobox and set your value path just like the display member
<ComboBox Name="Gen2Fis" ItemsSource="{Binding Path=Table}" SelectionChanged="Gen2Fis_SelectionChanged" DisplayMemberPath="LongName" SelectedValuePath="ShortName">
and then in the selectionchanged event you would be able to use selectedValue .
private void Gen2Fis_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string gen2fi = ((ComboBox)sender).SelectedValue ;
Gen2Str.ConnectString = gen2fi;
DisplayGen2Users();
}

WPF ComboBox binding behaviour

I have the following XAML markup:
<TextBox x:Name="MyTextBox" Text="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding Products}" DisplayMemberPath="ProductName"
SelectedValue="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber}"
SelectedValuePath="ProductNumber" />
My View's DataContext is bound to a viewmodel containing a public property called SelectedCustomer. Customer objects contain a FavouriteProduct property of type Product and Product objects contain public properties ProductNumber and ProductName.
The behaviour I'm looking for is to have the SelectedItem of the ComboBox update the Text in the TextBox and vice versa. ComboBox to TextBox works just fine. Selecting any product in the ComboBox updates the TextBox with the product number of that product. However when I try to go the other way I get som strange behaviour. It only works for the items that come before the selected item. I will try to explain:
Consider the following list of products ([Product Number], [Product Name]):
Fanta
Pepsi
Coca Cola
Sprite
Water
Now lets say that the SelectedCustomer's favourite product is Coca Cola (must be a developer). So when the window opens the TextBox reads 3 and the ComboBox reads Coca Cola. Lovely. Now lets change the product number in the TextBox to 2. The ComboBox updates it's value to Pepsi. Now try to change the product number in the TextBox to anything higher then the number for Coca Cola (3). Not so lovely. Selecting either 4 (Sprite) or 5 (Water) makes the ComboBox revert back to Coca Cola. So the behaviour seems to be that anything below the item that you open the window width from the list in the ItemSource does not work. Set it to 1 (Fanta) and none of the others work. Set it to 5 (Water) and they all work. Could this have to do with some initialisation for the ComboBox? Potential bug? Curious if anyone else have seen this behaviour.
UPDATE:
After reading Mike Brown's response I have created properties for SelectedProduct and SelectedProductNumber. The problem I am having with this is that as soon as you select something from the ComboBox you end up in an endless loop where the properties keep updatign each other. Have I implemented the OnPropertyChanged handler incorrectly or is there something I am missing? Here is a snippet of code from my ViewModel:
private int _SelectedProductNumber = -1;
public int SelectedProductNumber
{
get
{
if (_SelectedProductNumber == -1 && SelectedCustomer.Product != null)
_SelectedProductNumber = SelectedCustomer.Product.ProductNumber;
return _SelectedProductNumber;
}
set
{
_SelectedProductNumber = value;
OnPropertyChanged("SelectedProductNumber");
_SelectedProduct = ProductList.FirstOrDefault(s => s.ProductNumber == value);
}
}
private Product _SelectedProduct;
public Product SelectedProduct
{
get
{
if (_SelectedProduct == null)
_SelectedProduct = SelectedCustomer.Product;
return _SelectedProduct;
}
set
{
_SelectedProduct = value;
OnPropertyChanged("SelectedProduct");
_SelectedProductNumber = value.ProductNumber;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
UPDATE 2
I have changed the implementation slightly now by updating the SelectedCustomer.FavouriteProduct from both properties and then using that when reading their values. This now works but I'm not sure it's the 'correct way'.
private int _SelectedProductNumber = 0;
public int SelectedProductNumber
{
get
{
if (SelectedCustomer.Product != null)
_SelectedProductNumber = SelectedCustomer.Product.ProductNumber;
return _SelectedProductNumber;
}
set
{
_SelectedProductNumber = value;
SelectedCustomer.FavouriteProduct = ProductList.FirstOrDefault(s => s.ProductNumber == value);
OnPropertyChanged("SelectedProductNumber");
OnPropertyChanged("SelectedProduct");
}
}
private Product _SelectedProduct;
public Product SelectedProduct
{
get
{
if (SelectedCustomer.Product != null)
_SelectedProduct = SelectedCustomer.Product;
return _SelectedProduct;
}
set
{
_SelectedProduct = value;
SelectedCustomer.FavouriteProduct = value;
OnPropertyChanged("SelectedProduct");
OnPropertyChanged("SelectedProductNumber");
}
}
Your aim is not too clear so I have written the folloiwng so support either options I can see.
To keep two elements bound to one item in sync you can set the IsSynchronizedWithCurrentItem="True" on your combobox as shown below:
<TextBox x:Name="MyTextBox" Text="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding Products}" DisplayMemberPath="ProductName"
SelectedValue="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber}"
IsSynchronizedWithCurrentItem="True"
SelectedValuePath="ProductNumber" />
This will mean everything in the current window bound to the same background object will keep in sync and not give the odd behaviours you are seeing.
This quote form this longer MSDN article describes the effect:
The IsSynchronizedWithCurrentItem
attribute is important in that, when
the selection changes, that is what
changes the "current item" as far as
the window is concerned. This tells
the WPF engine that this object is
going to be used to change the current
item. Without this attribute, the
current item in the DataContext won't
change, and therefore your text boxes
will assume that it is still on the
first item in the list.
Then setting the Mode=TwoWay as suggested by the other answer will only ensure that both when you update the textbox the underlying object will be updated and when you update the object the textbox is updated.
This makes the textbox edit the selected items text and not select the item in the combolist with the matching text (which is the alternative think you are may be trying to achieve?)
To achieve the synchronised selection effect it may be worth setting IsEditable="True" on the combobox to allow users to type items in and dropping the text box. Alternatively if you need two boxes replace the textbox with a second combobox with IsSynchronizedWithCurrentItem="True" and IsEditable="True" then a styled to make it like a text box.
What you want to do is expose separate properties on your ViewModel for the currently selected product and currently selected product number. When the selected product is changed, update the product number and vice versa. So your viewmodel should look something like this
public class MyViewModel:INotifyPropertyChanged
{
private Product _SelectedProduct;
public Product SelectedProduct
{
get { return _SelectedProduct; }
set
{
_SelectedProduct = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProduct"));
_SelectedProductID = _SelectedProduct.ID;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProductID"));
}
}
private int _SelectedProductID;
public int SelectedProductID
{
get { return _SelectedProductID; }
set
{
_SelectedProductID = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProductID"));
_SelectedProduct = _AvailableProducts.FirstOrDefault(p => p.ID == value);
PropertyChanged(this,new PropertyChangedEventArgs("SelectedProduct"));
}
}
private IEnumerable<Product> _AvailableProducts = GetAvailableProducts();
private static IEnumerable<Product> GetAvailableProducts()
{
return new List<Product>
{
new Product{ID=1, ProductName = "Coke"},
new Product{ID = 2, ProductName="Sprite"},
new Product{ID = 3, ProductName = "Vault"},
new Product{ID=4, ProductName = "Barq's"}
};
}
public IEnumerable<Product> AvailableProducts
{
get { return _AvailableProducts; }
}
private Customer _SelectedCustomer;
public Customer SelectedCustomer
{
get { return _SelectedCustomer; }
set
{
_SelectedCustomer = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedCustomer"));
SelectedProduct = value.FavoriteProduct;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
So now your XAML binds to the appropriate properties and the viewModel is responsible for syncrhronization
<TextBox
x:Name="MyTextBox"
Text="{Binding Path=SelectedProductID, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox
x:Name="MyComboBox"
ItemsSource="{Binding AvailableProducts}"
DisplayMemberPath="ProductName"
SelectedItem="{Binding SelectedProduct}" />
Don't forget to implement the rest of INotifyPropertyChanged and the GetAvailableProducts function. Also there may be some errors. I hand typed this here instead of using VS but you should get the general idea.
Try:
SelectedItem="{Binding Path=YourPath, Mode=TwoWay"}
instead of setting SelectedValue and SelectedValuePath.
Might work with SelectedValue too, don't forget the Mode=TwoWay, since this isn't the default.
A good approuch would to use the master detail pattern - bind the master (the items view, e.g. combobox) to the data source collection and the detail view (e.g. text box) to the selected item in the source collection, using a binding converter to read/write the appropriate property.
Here is an example:
http://blogs.microsoft.co.il/blogs/tomershamam/archive/2008/03/28/63397.aspx
Notice the master binding is of the form {Binding} or {Binding SourceCollection} and the details binding is of the form {Binding } or {Binding SourceCollection}.
To get this working you need to wrap you collection with an object that keeps the selected item. WPF has one of these built-in: ObjectDataProvider.
Example:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/068977c9-95a8-4b4a-9d38-b0cc36d06446

WPF TextBox.Focus() annoyance

I'm very new to .Net and WPF and have a problem. The code is a snippet. I have TextBoxes to enter dates. I check on correct input using GotFocus and LostFocus events.
<TextBox Name="sdDay" Width="40" Text="Day" GotFocus="DateDay_GotFocus" LostFocus="DateDay_LostFocus" Padding="5,5,5,5" HorizontalContentAlignment="Center" Focusable="True"/>
<TextBox Name="sdMonth" Width="50" Text="Month" GotFocus="DateMonth_GotFocus" LostFocus="DateMonth_LostFocus" Padding="5,5,5,5" Margin="5,0,0,0" HorizontalContentAlignment="Center" Focusable="True"/>
<TextBox Name="sdYear" Width="50" Text="Year" GotFocus="DateYear_GotFocus" LostFocus="DateYear_LostFocus" Padding="5,5,5,5" Margin="5,0,0,0" HorizontalContentAlignment="Center" Focusable="True"/>
And the code:
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == "Day")
((TextBox)sender).Text = string.Empty;
}
private void DateDay_LostFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == string.Empty)
((TextBox)sender).Text = "Day";
else
CheckForCorrectDateDay((TextBox)sender);
}
private void CheckForCorrectDateDay(TextBox b)
{
int day = 0;
try
{
day = int.Parse(b.Text);
if (day < 0 || day > 31)
{
MessageBox.Show("Please enter a correct day.");
b.Text = string.Empty;
b.Focus();
}
}
catch (FormatException)
{
MessageBox.Show("Please enter a number.", "Incorrect Input", MessageBoxButton.OK, MessageBoxImage.Warning);
b.Text = string.Empty;
b.Focus();
}
catch (Exception)
{
throw;
}
}
Now what I want it to do is check for correct input, and if that fails, set the focus back to whatever TextBox had an incorrect entry.
It doesn't work though. After I enter a number outside the range (or letter), the MessageBox will show but the focus shifts to the next TextBox which is for entering the month.
What am I doing wrong?
Your technique for validation here is, to be frank, very poor. That said, I believe the problem is just that WPF is handling the tab after you've set focus, so it is setting focus back to the next item in the focus order.
A simple workaround would be to dispatch a separate message that is processed after the current message:
if (day < 0 || day > 31)
{
MessageBox.Show("Please enter a correct day.");
b.Text = string.Empty;
Dispatcher.BeginInvoke((ThreadStart)delegate
{
b.Focus();
});
}
Doing this ensures that WPF completely processes the LostFocus event handler before it processes the separate message to set focus on the erroneous control.
In terms of how you could tackle this problem in a much nicer way, you could:
Define a view model with properties for Day, Month, and Year (prerequisite: read up on the MVVM pattern)
Implement IDataErrorInfo on the view model
Bind the TextBoxes in the UI to the corresponding properties on the view model (prerequisite: read up on WPF data binding)

Resources