I have a Combobox with a concate string in displaymemberpath ('DescriptionComplete' in code-behind below), and that is what i get in the editable field BUT this is only the IdEchantillon that i want into the editable field...
How must i do please ?
XAML:
<ComboBox x:Name="cbEchantillon" SelectedValue="{Binding CurrentEchantillon.IdEchantillon}" ItemsSource="{Binding OcEchantillon}" DisplayMemberPath="DescriptionComplete" SelectedValuePath="IdEchantillon" SelectedItem="{Binding CurrentEchantillon}" Text="{Binding IdEchantillon}" IsEditable="True" Width="355" FontSize="14" FontFamily="Courier New" SelectionChanged="cbEchantillon_SelectionChanged"></ComboBox>
Code behind:
public class Echantillon : ViewModelBase
{
private string _IdEchantillon;
private string _Description;
private DateTime _dateechan;
private string _descriptionComplete;
}
public string IdEchantillon
{
get { return _IdEchantillon; }
set { _IdEchantillon = value; RaisePropertyChanged("IdEchantillon"); }
}
public string Description
{
get { return _Description; }
set { _Description = value; RaisePropertyChanged("Description"); }
}
public DateTime Dateechan
{
get { return _dateechan; }
set { _dateechan = value; RaisePropertyChanged("Dateechan"); }
}
public string DescriptionComplete
{
get { return string.Format("{0} {1} {2}", IdEchantillon.PadRight(20), Dateechan.ToShortDateString(), Description); }
set { _descriptionComplete = value; }
}
}
At first, please, cleanup your ComboBox property bindings. You should not create binding for SelectedValue, because you already have binding for SelectedItem property. The SelectedValue is determined by extracting the value by SelectedValuePath from the SelectedItem.
In case of binding to ViewModel, you should not also bind Text property. Use ItemsSource as you do, and set DisplayMemberPath property to what you want to show as the text representation of the every item in the bound collection.
Related
I need to update a label with data calculated from ViewModel. This must be trigged when a textBox is updated because I will use the data from textBox to calcualate the text that must be showed in the label.
My .xaml files is:
...
<TextBox x:Name="tbSelectedValue"
PreviewTextInput="SelectedValue_PreviewTextInput"
KeyUp="SelectedValue_KeyUp"
Text="{Binding Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SelectFileCommand}" />
</TextBox.InputBindings>
</TextBox>
<Label x:Name="lbSelectedFileName"
Content="{Binding Path=SelectedName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
...
My ViewModel File is:
public string SelectedValue
{
get { return selectedValue; }
set { SetProperty(ref selectedValue, value); }
}
public string SelectedName
{
get { return selectedName; }
set { selectedName = value; }
}
internal string GetSelectName()
{
try
{
selectedValue = (selectedValue == "" ? "" : GetFileByNumber(Int32.Parse(selectedValue)).name);
return selectedValue;
}
catch (Exception e)
{
return "Nenhum arquivo encontrado";
}
}
The SelectedValue works, but SelectedName don't work.
I need to call the function GetSelectName when the value in textBox is updated (selectedValue). My function GetSelectName update the selectedName property and it must be updated in the view. But it's not working.
What I must do?
As per #EdPlunkett comments let me put this together and see if it helps.
Modify SelectedName so it can notify property changes.
public string SelectedName
{
get { return selectedName; }
set {
SetProperty(ref selectedName, value);
}
}
Assign the result of the GetSelectName() method to SelectedName
property.
public string SelectedValue
{
get { return selectedValue; }
set
{
if (SetProperty(ref selectedValue, value))
{
//If property value changes, update the name property as well
SelectedName = GetSelectedName();
}
}
}
Does it help?
So I have very simple combobox containing list of values. I am supposed to bind the selectedvalue to a viewmodel property and store it in DB. Below is my current approach:
SampleViewModel.cs
public class SampleViewModel: BindableBase
{
public SampleViewModel()
{
MyDetails = new ObservableCollection<DetailItems>(){
new DetailItems{Name="Detail1"},
new DetailItems{Name = "Detail2"},
new DetailItems{Name= "Detail3"},
new DetailItems{Name="Detail4"}
};
}
private ObservableCollection<DetailItems> _myDetails;
private string _myDetail;
public ObservableCollection<DetailItems> MyDetails { get { return _myDetails; } set { SetProperty(ref _myDetails, value); } }
public string MyDetail { get { return _myDetail; } set { SetProperty(ref _myDetail, value); } }
}
public class DetailItems: BindableBase
{
private string _name;
public string Name { get { return _name; } set { SetProperty(ref _name, value); } }
}
and my ComboBox in View is as follows
<ComboBox x:Name="cbDetails"
ItemsSource="{Binding MyDetails}"
DisplayMemberPath="Name"
SelectedValuePath="{Binding Path=Name}"
SelectedValue="{Binding MyDetail}"/>
But whenever I get data in backend, the string MyDetail will have an instance of DetailItems converted to string. Could anyone let me know how I can change this to bind appropriate value to MyDetail?
The reason is quite simple: SelectedValuePath expects path to the property of object, not binding (just like DisplayMemberPath does). So a fix would be:
<ComboBox x:Name="cbDetails"
ItemsSource="{Binding MyDetails}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding MyDetail}"/>
Obviously, SelectedValue always holds the currently selected item only. So you have to change the type from string to DetailItems as follow,
private DetailItems_myDetail;
public DetailItems MyDetail { get { return _myDetail; } set { SetProperty(ref _myDetail, value); } }
}
if you want the selected name call MyDetail.Name it will return your string
Here's my issue...I have a list of servers, each with an ID and ServerName. I want to be able to select a server from the ComboBox and edit it in place, then have its ID available to update via SQL later. So let's say this is the data: (ID=1, Name="Server1"), (ID=2, Name="Server2"), (ID=3, Name="Server3"). If I select Server3 from the ComboBox, I'd like to edit it to be "Server4" then upload that with a SQL query (I know how to do this part). I'm utilizing MVVM, so all the values are properties of my ViewModel.
Currently, when the text field is modified in the ComboBox the SelectedServer immediately becomes null, presumably because it is no longer a value it recognizes. I could use some guidance on how to get this to do what I'm trying to do.
<ComboBox Grid.Column="1" x:Name="serverNameUpdateBox" HorizontalAlignment="Stretch" Height="23" VerticalAlignment="Center" IsEditable="True"
ItemsSource="{Binding Path=DataContext.SelectedProjectServers, ElementName=main}"
DisplayMemberPath="ServerName"
SelectedValue="{Binding SelectedServer}"
SelectedValuePath="ServerName"
Text="{Binding SelectedServer.ServerName, UpdateSourceTrigger=LostFocus}"
/>
And ViewModel relevant code:
namespace ViewModel
{
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
SelectedProjectServers = new List<Server>();
SelectedServer = new Server();
private Server _selectedServer;
public Server SelectedServer
{
get { return _selectedServer; }
set
{
if (value == null) { ModifiedServer = _selectedServer; }
_selectedServer = value;
RaisePropertyChanged("SelectedServer");
}
}
private List<Server> _selectedProjectServers;
public List<Server> SelectedProjectServers
{
get { return _selectedProjectServers; }
set
{
_selectedProjectServers = value;
RaisePropertyChanged();
}
}
}
}
}
And Model relevant code:
namespace Model
{
public class Server : INotifyPropertyChanged
{
private string _serverName;
public string ServerName
{
get { return _serverName; }
set
{
_serverName = value;
RaisePropertyChanged();
}
}
private int _serverID;
public int ServerID
{
get { return _serverID; }
set
{
_serverID = value;
RaisePropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string caller = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
}
}
}
Bind a property like "EditedServerName" to Combobox.Text.
When the "EditedServerName" is changed you can set the value to the "ServerName" of your SelectedServer.
<ComboBox Grid.Column="1" x:Name= "serverNameUpdateBox" HorizontalAlignment= "Stretch" Height= "23" VerticalAlignment= "Center" IsEditable= "True"
ItemsSource= "{Binding Path=DataContext.SelectedProjectServers, ElementName=main}"
DisplayMemberPath= "ServerName"
SelectedItem="{Binding SelectedServer}"
Text= "{Binding EditedServerName, UpdateSourceTrigger=LostFocus}"
/>
ComboBox is primarily used for selection. You could have used another control like datagrid or so for update functionality.
Well, if you want to do it the ComboBox way, I suggest that you place a couple of text boxes below your combo box and bind these text boxes content to the SelectedServer properties i.e.
<TextBox x:name="ServerName" Text ={Binding SelectedServer.ServerName} />
and so on.
So, when ever a server is selected, these text boxes will be filled with values from currently selected Server. And then, you can trigger some command using a button below these boxes which triggers the sql query and passes the required data using bound properties from text boxes.
I hope you got the idea.
Tried all the solutions for similar issues around here, still no go. I have a ComboBox that should work for selection of existing items and/or for adding new ones. Only the selected item part works. Category is just an object with a Name and Id.
Thanks in advance!
XAML
<ComboBox Name="CbCategory" ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory.Name, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding NewCategory.Name}" DisplayMemberPath="Name"
IsEditable="True"/>
Code behind
private Category _selectedCategory;
public Category SelectedCategory
{
get { return _selectedCategory; }
set
{
if (Equals(_selectedCategory, value)) return;
_selectedCategory = value;
SendPropertyChanged("SelectedCategory");
}
}
private Category _newCategory;
public Category NewCategory
{
get { return _newCategory; }
set
{
if (Equals(_newCategory, value)) return;
_newCategory = value;
SendPropertyChanged("NewCategory");
}
}
Your Text Binding doesn't work because you're binding against a null Category property. Instantiate it instead.
public Category NewCategory
{
get { return _newCategory ?? (_newCategory = new Category()); }
set
{
if (Equals(_newCategory, value)) return;
_newCategory = value;
SendPropertyChanged("NewCategory");
}
}
Edit: Elaborating as per your comment:
Your ComboBox.Text binding is set to "{Binding NewCategory.Name}", so no matter what the value of SelectedCategory is, the Text property will always reflect the name of the NewCategory.
When NewCategory is null, the Text property has nothing to bind to, and therefore the 2-way binding cannot be executed (that is, the value of the Text property cannot be passed back to NewCategory.Name, because that will cause an NullReferenceException (because the NewCategory is null).
This does not affect the case of the SelectedItem, because that's binding directly to the SelectedCategory property, and not a sub-property of that.
Create new varible to keep text of combobox. If the selectedItem having null value get the text of combobox as new Item,
Code :
<ComboBox Name="CbCategory" ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory.Name, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Name}" DisplayMemberPath="Name"
IsEditable="True"/>
private String _name;
public Category Name
{
get { return _name; }
set
{
_name = value
SendPropertyChanged("Name");
}
}
public ICommand ItemChange
{
get
{
`return new RelayCommand(() =>`{
try{string item = this.SelectedCategory.Code;}
catch(Exception ex){string item = this.Name;}
}, () => { return true; });
}
}
In my View I have a slider and a combobox.
When I change the slider, I want the combobox to change.
When I change the combobox, I want the slider to change.
I can udpate one or the other, but if I try to update both I get a StackOverflow error since one property keeps updating the other in an infinite loop.
I've tried putting in a Recalculate() where the updating is done in one place, but still run into the recursion problem.
How can I have each control update the other without going into recursion?
in View:
<ComboBox
ItemsSource="{Binding Customers}"
ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
Margin="20"
HorizontalAlignment="Left"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>
<Slider Minimum="0"
Maximum="{Binding HighestCustomerIndex, Mode=TwoWay}"
Value="{Binding SelectedCustomerIndex, Mode=TwoWay}"/>
in ViewModel:
#region ViewModelProperty: SelectedCustomer
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get
{
return _selectedCustomer;
}
set
{
_selectedCustomer = value;
OnPropertyChanged("SelectedCustomer");
SelectedCustomerIndex = _customers.IndexOf(_selectedCustomer);
}
}
#endregion
#region ViewModelProperty: SelectedCustomerIndex
private int _selectedCustomerIndex;
public int SelectedCustomerIndex
{
get
{
return _selectedCustomerIndex;
}
set
{
_selectedCustomerIndex = value;
OnPropertyChanged("SelectedCustomerIndex");
SelectedCustomer = _customers[_selectedCustomerIndex];
}
}
#endregion
try in the set functions something like:
public int SelectedCustomerIndex
{
get
{
return _selectedCustomerIndex;
}
set
{
if (value != _selectedCustomerIndex)
{
_selectedCustomerIndex = value;
OnPropertyChanged("SelectedCustomerIndex");
SelectedCustomer = _customers[_selectedCustomerIndex];
}
}
}
to fire the events only when there is an actual change in value. This way, a second call to the set property with the same value does not cause another change event.
You have to do that for the other property as well of course.
Both properties are called from each other, hence the recursion. Not related to binding at all. Proper way is to change each other and fire change notification for both properties when either property changes:
public Customer SelectedCustomer
{
get
{
return _selectedCustomerIndex;
}
set
{
_selectedCustomer = value;
_selectedCustomerIndex = _customers.IndexOf(value);
OnPropertyChanged("SelectedCustomer");
OnPropertyChanged("SelectedCustomerIndex");
}
}
public int SelectedCustomerIndex
{
get
{
return _selectedCustomerIndex;
}
set
{
_selectedCustomerIndex = value;
_selectedCustomer = _customers[_selectedCustomerIndex];
OnPropertyChanged("SelectedCustomer");
OnPropertyChanged("SelectedCustomerIndex");
}
}