I am binding a combobox with a varaible on my PLC so when i change the combobox the changed value transmitted to my PLC but when i change on my PLC the combobox wont change even if the field is changed
I put a brakepoint on the field(the one i bind to SelectedItem) to check if it changes and it did but didn't affect the combobox
Here is my ViewModel
List<string> _temperatureList = new List<string> { "80 °C", "100 °C", "120 °C" };
public List<string> TemperatureList
{
get { return _temperatureList; }
}
public string Temperature
{
get
{
return (TemperatureGS != 0) ? string.Format("{0} °C", TemperatureGS) : "80 °C";
}
set
{
TemperatureGS = Convert.ToSByte(value.Replace(" °C", ""));
OnPropertyChanged();
WriteTemperature(TemperatureGS);
}
}
private short _temperature ;
public short TemperatureGS
{
get { return _temperature; }
set { SetProperty(ref _temperature, value); }
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
TemperatureGS = PLCread(0);
OnPropertyChanged(Temperature);
}
My Xaml code
<ComboBox SelectedItem="{Binding Temperature, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
ItemsSource="{Binding TemperatureList }" />
Related
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
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.
Friends,
I am using MVVM to bind ListBox SelectedItem but seems it doesn't work. The list is binded successfully, but No item is selected.
XAML Page
<ListBox x:Name="lsbReadingChapter" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
ItemsSource="{Binding QuranText}" Style="{StaticResource ListBoxStyle1}"
Grid.Row="1">
....
</ListBox>
MVVM Code
public class QuranTextViewModel : INotifyPropertyChanged
{
DataSource ds = null;
ObservableCollection<ArabicTextWithTranslation> _quranText;
public QuranTextViewModel(Chapter c)
{
ds = new DataSource();
QuranText = new ObservableCollection<ArabicTextWithTranslation>();
List<ArabicTextWithTranslation> texts = ds.getArabicTextWithTranslation(c);
Recent r = DataSource.Recents;
SelectedItem = (from grp in texts
where grp.ArabicText.ChapterID == r.ChapterID && grp.ArabicText.AyaID == r.AyaID
select grp).FirstOrDefault();
foreach (ArabicTextWithTranslation t in texts)
{
QuranText.Add(t);
}
}
public ObservableCollection<ArabicTextWithTranslation> QuranText
{
get { return _quranText; }
set
{
if (_quranText != value)
{
_quranText = value;
OnPropertyChanged("QuranText");
}
}
}
ArabicTextWithTranslation _selectedItem;
public ArabicTextWithTranslation SelectedItem
{
get {
return _selectedItem;
}
set
{
if (_selectedItem != value)
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
}
}
The SelectedItem is not working, though in debug mode I can see the value in SelectedItem.
How to set SelectedItem and Highlight the SelectedItem?
I want change number from ComboBox and change value in TextBox.
(For example I have number =2 and content at "lblblblb" ofc. this is in ObservableCollection<string>, so I to call ContentWithListView[SelectNumberStep])
ReadPage.xaml
<TextBox HorizontalAlignment="Left" Margin="580,154,0,0"
TextWrapping="Wrap" Text="{Binding ContentWithListView[SelectNumberStep],Mode=TwoWay}"
VerticalAlignment="Top" Width="725" Height="82"/>
<ComboBox HorizontalAlignment="Left" Margin="440,154,0,0"
ItemsSource="{Binding NumberStep,Mode=TwoWay}"
SelectedItem="{Binding SelectNumberStep,Mode=TwoWay}"
VerticalAlignment="Top" Width="95" Height="77" />
How I change content in TextBox from CombBox numbers?
ReadViewModel.cs
private ObservableCollection<string> contentWithListView;
public ObservableCollection<string> ContentWithListView
{
get
{
return this.contentWithListView;
}
set
{
this.contentWithListView = value;
}
}
private ObservableCollection<int> stepNumber;
public ObservableCollection<int> NumberStep
{
get
{
return this.stepNumber;
}
set
{
this.stepNumber = value;
}
}
private int selectNumberStep;
public int SelectNumberStep
{
get
{
return this.selectNumberStep;
}
set
{
this.selectNumberStep = value;
}
}
previous answer doesn't integrate the fact he textbox content have to be in TwoWays so, in such scenario, you could consolidate your properties with the INotifyPropertyChanged interface like that:
Xaml part
<StackPanel d:DataContext="{d:DesignInstance Type=classes:StackOverFlowX }">
<TextBox Text="{Binding Content, Mode=TwoWay}"/>
<ComboBox ItemsSource="{Binding NumberStep, Mode=TwoWay}"
SelectedItem="{Binding SelectNumberStep,Mode=TwoWay}"/>
</StackPanel>
Class
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class StackOverFlowX : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public StackOverFlowX()
{
}
private ObservableCollection<string> contentWithListView;
public ObservableCollection<string> ContentWithListView
{
get
{
return this.contentWithListView;
}
set
{
this.contentWithListView = value;
OnPropertyChanged();
}
}
private ObservableCollection<int> stepNumber;
public ObservableCollection<int> NumberStep
{
get
{
return this.stepNumber;
}
set
{
this.stepNumber = value;
OnPropertyChanged();
}
}
private int selectNumberStep;
public int SelectNumberStep
{
get
{
return this.selectNumberStep;
}
set
{
this.selectNumberStep = value;
OnPropertyChanged();
OnPropertyChanged("Content");
}
}
private string _content;
public string Content
{
get
{
return contentWithListView[this.SelectNumberStep];
}
set
{
this._content = value;
if (contentWithListView.IndexOf(value) > -1)
{
SelectNumberStep = contentWithListView.IndexOf(value);
OnPropertyChanged("SelectNumberStep");
}
OnPropertyChanged();
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I would change your view model code around, so that the text box binds to a scalar string value which is updated on the SelectNumberStep change:
public string Content
{
get
{
// bounds checking here..
return contentWithListView[this.SelectNumberStep];
}
}
public int SelectNumberStep
{
get
{
return this.selectNumberStep;
}
set
{
this.selectNumberStep = value;
this.NotifyOfPropertyChange(() => this.SelectNumberStep);
this.NotifyOfPropertyChange(() => this.Content);
}
}
<TextBox Text="{Binding Content}" ... />
Why is that whenever I try to put SlectedIndex to 0, it always remains -1 ?
public partial class Window1 : Window
{
private ObservableCollection<string> _dropDownValues = new ObservableCollection<string>();
public ObservableCollection<string> DropDownValues
{
get { return _dropDownValues; }
set { _dropDownValues = value; }
}
private string _selectedValue;
public string SelectedValue
{
get { return _selectedValue; }
set { _selectedValue = value; }
}
public Window1()
{
InitializeComponent();
DataContext = this;
DropDownValues.Add("item1");
DropDownValues.Add("item2");
DropDownValues.Add("item3");
DropDownValues.Add("item4");
DropDownValues.Add("item5");
DropDownValues.Add("item6");
if (combotest.SelectedIndex == -1)
{
combotest.SelectedIndex = 0;
}
}
}
<StackPanel HorizontalAlignment="Left" Margin="10">
<ComboBox Name="combotest"
Margin="0 0 0 5"
ItemsSource="{Binding DropDownValues}"
SelectedValue="{Binding SelectedValue}"
Width="150"/>
</StackPanel>
Please correct me if I am wrong, but you havent set the SelectedValuePath in your XAML. Also once you set SelectedValuePath, you only need to set the default SelectedValue (same as the first item's value property from your items source) and there is no need for your SelectedIndex code.
Let me know if this helps.
Try this instead of setting the index.
string s = DropDownValues[0];
SelectedItem = s;