I have just started using WPF and I am facing a problem displaying data to a DataGrid.
WPF (Views\ResultsPage.xaml):
<DataGrid x:Name="dgResults"
Margin="2"
Style="{DynamicResource AzureDataGrid}"
ItemsSource="{Binding SampleData}"
AutoGenerateColumns="False"
RenderOptions.ClearTypeHint="Enabled"
TextOptions.TextFormattingMode="Display">
<DataGrid.Columns>
<DataGridTextColumn Header="Well" Binding="{Binding WellID}"/>
..
C# (Data\SampleData.cs):
public class : INotifyPropertyChanged
{
private string _wellID;
public string WellID
{
get
{
return _wellID;
}
set
{
_wellID = value;
NotifyPropertyChanged("WellID");
}
}
...
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
I get this error: System.Windows.Data Error: 40 : BindingExpression path error: 'SampleData'
SampleData is supposed to be a property of the DataContext of the DataGrid (or a parent element) that returns an IEnumerable<SampleData>. Try this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//set the DataContext of the window itself or the DataGrid
dgResults.DataContext = new ViewModel();
}
}
public class ViewModel
{
public ViewModel()
{
SampleData = new List<SampleData>();
SampleData.Add(new SampleData() { WellID = "1" });
SampleData.Add(new SampleData() { WellID = "2" });
}
public List<SampleData> SampleData { get; }
}
Related
I have ObservableCollection of DeviceInformation which is added in MainWindowViewModel and linked with DataContext.
public partial class MainWindow : Window
{
MainWindowViewModel viewModel = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = viewModel;
}
}
Here is the MainWindowViewModel:
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<DeviceInformation> allDeviceInfo = new ObservableCollection<DeviceInformation>();
public MainWindowViewModel()
{
// here some of the commands
}
public ObservableCollection<DeviceInformation> AllDeviceInfo
{
get { return allDeviceInfo; }
set
{
allDeviceInfo = value;
this.RaisePropertyChanged(nameof(AllDeviceInfo));
}
}
}
The RaisePropertyChanged is done with implementing ViewModelBase which looks like this:
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
this.RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void RaisePropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
}
Inside my DeviceInformation I have a List of SyntaxMessages:
public class DeviceInformation : ViewModelBase
{
private List<SyntaxMessages> list = new List<SyntaxMessages>();
private string test = "";
public List<SyntaxMessages> ComConsoleMessages{
get { return list; } // get method
set
{
list = value;
RaisePropertyChanged(nameof(ComConsoleMessages));
} // set method
}
public string Test{
get { return test; } // get method
set
{
test = value;
RaisePropertyChanged(nameof(Test));
} // set method
}
}
This is how the SyntaxMessages looks:
public class SyntaxMessages : ViewModelBase
{
#region private values
private string message = "";
private string status = "";
private string color = "Transparent";
#endregion
#region Public values
public string Message {
get { return message; }
set
{
message = value;
RaisePropertyChanged(nameof(Message));
}
}
public string Status {
get { return status; }
set
{
status = value;
RaisePropertyChanged(nameof(Status));
}
}
public string Color {
get { return color; }
set
{
color = value;
RaisePropertyChanged(nameof(Color));
}
}
#endregion
}
So when I running my program and connecting device to it will collect and add information the the DeviceInformation and this will be added to ObervableCollection of DeviceInformation. This will update my MainTabControl by adding new tab and binding many strings like "Test" (there is more then one) to the TextBoxes, and also update the SubTabControl which is inside the main one. Inside both of the TabItems inside SubTabControl I also have a ListView to which I want link the List of SyntaxMessages this looks like this:
<ListView
Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="5"
MinHeight="40"
Padding="0"
Margin="2"
ItemsSource="{Binding ComConsoleMessages, UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView
AllowsColumnReorder="False">
<GridViewColumn
DisplayMemberBinding="{Binding Message}"
Header="Message" />
<GridViewColumn
Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}" Foreground="{Binding Color}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Problem
All works fine except a ListView. When I add some SyntaxMessages to the List of SyntaxMessages called ComConsoleMessages then I have to switch between SubTabControl tabs(SubTabItem1/2) to see ListView updated. I want to update ListView every single time when new message is added to the List of SyntaxMessages which is inside DeviceInfromation which is indside ObservableCollection of DeviceInfromations that is linked via MainWindowViewMode to the window DataContext.
Here is the view:
I have a ComboBox in my View:
<ComboBox Name="comboBox1" ItemsSource="{Binding MandantList}" SelectedItem="{Binding CurrentMandant, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Firma}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here is my Model:
public class MandantListItem : INotifyPropertyChanged
{
public MandantListItem() { }
string _Firma;
bool _IsChecked;
public string Firma
{
get { return _Firma; }
set { _Firma = value; }
}
public bool IsChecked
{
get
{
return _IsChecked;
}
set
{
_IsChecked = value;
OnPropertyChanged(nameof(IsChecked));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
And here is my ViewModel:
public class MaViewModel : INotifyPropertyChanged
{
public ObservableCollection<MandantListItem> MandantList { get { return _MandantList; } }
public ObservableCollection<MandantListItem> _MandantList = new ObservableCollection<MandantListItem>();
private MandantListItem _CurrentMandant;
public MandantListItem CurrentMandant
{
get { return _CurrentMandant; }
set
{
if (value != _CurrentMandant)
{
_CurrentMandant = value;
OnPropertyChanged("CurrentMandant");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
How to fill the ComboBox:
public zTiredV2.ViewModel.MaViewModel MAList = new zTiredV2.ViewModel.MaViewModel();
this.comboBox1.ItemsSource = MAList.MandantList;
MAList.MandantList.Add(new zTiredV2.Model.MandantListItem { Firma = "A", Homepage = "a.com", IsChecked = false });
MAList.MandantList.Add(new zTiredV2.Model.MandantListItem { Firma = "B", Homepage = "b.com", IsChecked = false });
But my item doesnt update ... tried also via IsChecked, but no success either ... when i iterate through MAList, IsChecked is always false. And how can i bind a TextBlock to the selected Firma?
Have a hard time with MVVM, but i like it.
You should set the DataContext of the ComboBox to an instance of your view model. Otherwise the bindings won't work:
this.comboBox1.DataContext = MAList;
Also note that the _MandantList backing field for your property shouldn't be public. In fact, you don't need it at all:
public ObservableCollection<MandantListItem> MandantList { get; } = new ObservableCollection<MandantListItem>();
Setting the DataContext should cause the CurrentMandant property to get set when you select an item in the ComboBox. It won't set the IsChecked property though.
I have following textbox binding:
XAML:
<TextBlock x:Name="Auslastungskapazität1" Text="{Binding Kapazität, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="Black" HorizontalAlignment="Center" Margin="0,5,5,5" FontSize="16" ></TextBlock>
MainViewModel Class:
class MainViewModel: ZuliefererStandortListe, IDropTarget, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int Kapazität {
get { return _kapazität1Ausgelastet; }
set {
if (this._kapazität1Ausgelastet != value)
_kapazität1Ausgelastet = value;
OnPropertyChanged("Kapazität");
}
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
DataContext is the in the MainWindow Constructor as
Kapazität.DataContext = new MainViewModel();
If I change Kapazität the int get changed and the OnPropertyChanged() method gets called. However "PropertyChanged" remains null and therefore the Textbox Binding doesn't get updated.
Either set the DataContext of the TextBox itself:
Auslastungskapazität1.DataContext = new MainViewModel();
...or of any of its parent elements, such as for example the window:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
I am having a difficult time getting my WPF to properly use Databinding. In the XAML I have a the following:
....
<TextBox Name="txt_FirstName" Text="{Binding Path=currentApplication.FirstName, UpdateSourceTrigger=PropertyChanged}" />
....
I have in the following CS code:
namespace WPF1
{
public partial class MainWindow : Window
{
personalApp currentApplication = new personalApp ();
public MainWindow()
{
InitializeComponent();
}
}
}
That references the following two classes:
class personalApp : INotifyPropertyChanged
{
private Person person = new Person();
public string FirstName
{
get { return person.FirstName; }
set
{
person.FirstName = value;
this.OnPropertyChanged("FirstName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(
this, new PropertyChangedEventArgs(propName));
}
}
class Person
{
private string firstName = "";
get { return firstName; }
set { FirstName = value; }
}
I pause it in the code and step through to check, but when I update the txt_FirstName in the application, it never seems to set the firstName Object.
Where am I going wrong?
You need to update your XAML binding, and set the DataContext of the Window using the TextBox.
namespace WPF1
{
public partial class MainWindow : Window
{
personalApp currentApplication = new personalApp ();
public MainWindow()
{
InitializeComponent();
this.DataContext = currentApplication;
}
}
}
Updating the XAML:
<TextBox Name="txt_FirstName" Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}" />
I have corrected the code.
For text box:
<TextBox Name="txt_FirstName" Height="30" Background="Beige"
Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />
C# Code
namespace Wpf1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new personalApp();
}
}
internal class personalApp : INotifyPropertyChanged
{
private Person person = new Person();
public string FirstName
{
get { return person.FirstName; }
set
{
person.FirstName = value;
this.OnPropertyChanged("FirstName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(
this, new PropertyChangedEventArgs(propName));
}
}
internal class Person
{
public string FirstName { get; set; }
}
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
or if you don't want to assign data context to yourself (window), as you might have other datacontext coming into the window, you can add this in xaml:
give your window a name:
<Window .... x:Name="this"...
then
<TextBox Name="txt_FirstName" Text="{Binding ElementName=this,
Path=currentApplication.FirstName/>
What you mean by "update the txt_FirstName in the application" ?
If you set directly the value of the textbox then you should try to set the value of currentApplication instead of the textbox value
I am unable figure out why the databinding is not working as expected:
I created a Listbox and set its ItemSource to my observable collection
I used this.DataContext = this
I Initialized my public Observable Collection
I filled it with objects that implement INotifyPropertyChanged
Yet, the databinding, still does not work. My Listbox:
<ListBox Height="425" ItemsSource="{Binding headers}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=HeaderInfo}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The code behind:
public partial class cornet_controls : PhoneApplicationPage
{
public ObservableCollection<headerInfo> headers;
public cornet_controls()
{
InitializeComponent();
this.DataContext = this;
headers = new ObservableCollection<headerInfo>();
for (int x = 0; x < 100; x++)
headers.Add((new headerInfo() { HeaderInfo = x.ToString() }));
}
}
My custom class implementing INotifyPropertyChanged:
public class headerInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public headerInfo()
{}
private String _HeaderInfo;
public String HeaderInfo
{
get { return _HeaderInfo; }
set { _HeaderInfo = value; NotifyPropertyChanged("HeaderInfo"); }
}
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
You cannot bind to a NonProperty:
<ListBox Height="425" ItemsSource="{Binding headers}">
public ObservableCollection<headerInfo> headers;
you need to bind to a Property like:
public ObservableCollection<headerInfo> headers { get; set; }