WPF Combobox Binding Entity Framework - wpf

This is the class that the Entity Framework created from the Database.
namespace QuickTest
{
using System;
using System.Collections.Generic;
public partial class laender
{
public int idLaender { get; set; }
public string EU { get; set; }
public string FIBUPrefixKunde { get; set; }
public string FIBUPrefixLieferant { get; set; }
public string ISO2 { get; set; }
public string ISO3 { get; set; }
public string Land_DE { get; set; }
public string Land_EN { get; set; }
public Nullable<int> Vorwahl { get; set; }
public string KFZ { get; set; }
public string TLD { get; set; }
public Nullable<double> NeukundenLimitABC { get; set; }
}
}
And I simply want to show those values in a Datagrid (thats working)
But I want the EU property to be a combobox with just 2 simple options "Yes" and "No"
So there is no foreign Key to some other Table or something. When I am binding the SelectedItem to EU it just shows nothing and I dont know why
Here is the XAML of the DataGrid
<DataGrid x:Name="laenderDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="58,48,59,72" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="eUColumn" Header="EU" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding EU}">
<ComboBoxItem Content="YES"/>
<ComboBoxItem Content="NO"/>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="fIBUPrefixKundeColumn" Binding="{Binding FIBUPrefixKunde}" Header="FIBUPrefix Kunde" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="fIBUPrefixLieferantColumn" Binding="{Binding FIBUPrefixLieferant}" Header="FIBUPrefix Lieferant" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="idLaenderColumn" Binding="{Binding idLaender}" Header="id Laender" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="iSO2Column" Binding="{Binding ISO2}" Header="ISO 2" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="iSO3Column" Binding="{Binding ISO3}" Header="ISO 3" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="kFZColumn" Binding="{Binding KFZ}" Header="KFZ" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="land_DEColumn" Binding="{Binding Land_DE}" Header="Land DE" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="land_ENColumn" Binding="{Binding Land_EN}" Header="Land EN" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="neukundenLimitABCColumn" Binding="{Binding NeukundenLimitABC}" Header="Neukunden Limit ABC" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="tLDColumn" Binding="{Binding TLD}" Header="TLD" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="vorwahlColumn" Binding="{Binding Vorwahl}" Header="Vorwahl" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
There is no binding error or any other error it's just not showing
EDIT
I got it to work I had to set the Binding to SelectedValue and SelectedValuePath to Content because I want the COntent of the Comboboxitem
SelectedValuePath="Content" SelectedValue="{Binding EU}

Related

Binding ObservableCollection to DataGrid

How do I bind a ObservableCollection<updateData> updateCollection to a DataGrid? I tried several solution but none seem to work as rows are added to the collection but don't show up on the grid. I tried to bind to the class only, then I can add rows but when I try to edit them i get the error 'EditItem' is not allowed for this view. The grid is the following
<DataGrid Name="dgv" Grid.ColumnSpan="7" AutoGenerateColumns="False" ItemsSource="{Binding updateCollection}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Hour" SelectedValueBinding="{Binding Active}" ItemsSource="{StaticResource hoursList}" DisplayMemberPath="Key" SelectedValuePath="Value"/>
<DataGridComboBoxColumn Header="Origin" SelectedValueBinding="{Binding Origin}" ItemsSource="{StaticResource originList}" DisplayMemberPath="Key" SelectedValuePath="Value"/>
<DataGridTextColumn Header="P" Binding="{Binding Path=Price}"/>
<DataGridTextColumn Header="Q" Binding="{Binding Path=Quantity}"/>
</DataGrid.Columns>
And the updateData class is the following:
public class updateData
{
public string Price { get; set; }
public string Quantity { get; set; }
public string Origin { get; set; }
public string Hour { get; set; }
}
What you did looks correct, but if you miss one single thing, the DataContext… nothing will work.
Here an example just for you:
This is your Model:
public class updateData
{
public string Price { get; set; }
public string Quantity { get; set; }
public string Origin { get; set; }
public string Hour { get; set; }
}
Note that, if you want to tell to your view that something has changed, you have to implement the INotifyPropertyChanged interface.
This is your ViewModel:
public class updateDataVm
{
public ObservableCollection<updateData> updateCollection { get; set; }
public updateDataVm()
{
updateCollection = new ObservableCollection<updateData>();
}
}
And finally here is your View (note that i have changed ItemsSource to ItemsSource="{Binding}"):
<Grid>
<DataGrid Name="dgv" Grid.ColumnSpan="7" AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Hour" SelectedValueBinding="{Binding Active}" ItemsSource="{StaticResource hoursList}" DisplayMemberPath="Key" SelectedValuePath="Value"/>
<DataGridComboBoxColumn Header="Origin" SelectedValueBinding="{Binding Origin}" ItemsSource="{StaticResource originList}" DisplayMemberPath="Key" SelectedValuePath="Value"/>
<DataGridTextColumn Header="P" Binding="{Binding Path=Price}"/>
<DataGridTextColumn Header="Q" Binding="{Binding Path=Quantity}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
In your window (or generally control):
public partial class MainWindow : Window
{
public updateDataVm collection;
public MainWindow()
{
InitializeComponent();
collection = new updateDataVm();
DataContext = collection;
}
}

MVVM Light DataGrid binding

I'm developing a WPF with C#, .NET Framework 4.5.1, MVVM Light framework and Entity Framework 6.1.3.
I have this DataGrid in a window:
<DataGrid x:Name="LevelConfigurationDataGrid" Grid.Column="1" Grid.Row="1" Margin="20,0" ItemsSource="{Binding LevelConfigs}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding CODE_LEVE}" Header="Nivel"/>
<DataGridTextColumn Binding="{Binding CODE_NAME}" Header="Nombre"/>
<DataGridTextColumn Binding="{Binding IS_ACTIVE}" Header="¿Activa?"/>
<DataGridTextColumn Binding="{Binding CODE_TYPE}" Header="Tipo de código"/>
<DataGridTextColumn Binding="{Binding CODE_SOURCE}" Header="Origen del código"/>
<DataGridTextColumn Binding="{Binding HELPER_CODE_TYPE}" Header="Tipo de Helper Code"/>
<DataGridTextColumn Binding="{Binding HELPER_CODE_SOURCE}" Header="Origen del helper code"/>
<DataGridTextColumn Binding="{Binding QUANTITY}" Header="Cantidad"/>
<DataGridTextColumn Binding="{Binding REMAINING_CODES}" Header="Restantes"/>
<DataGridTextColumn Binding="{Binding TRZ_ENDPOINT}" Header="TRZ IP"/>
<DataGridTextColumn Binding="{Binding TRZ_ENDPORT}" Header="TRZ Puerto"/>
</DataGrid.Columns>
</DataGrid>
And on MainViewModel:
public ObservableCollection<CODE_LEVEL_CONFIGURATION> LevelConfigs
{
get { return m_LevelConfigs; }
set
{
m_LevelConfigs = value;
RaisePropertyChanged("LevelConfigs");
}
}
And on CODE_LEVEL_CONFIGURATION:
public class CODE_LEVEL_CONFIGURATION
{
public byte CODE_LEVEL { get; set; }
public string LEVEL_NAME { get; set; }
public bool IS_ACTIVE { get; set; }
public byte CODE_TYPE { get; set; }
public byte CODE_SOURCE { get; set; }
public byte? HELPER_CODE_TYPE { get; set; }
public byte? HELPER_CODE_SOURCE { get; set; }
public int QUANTITY { get; set; }
public int REMAINING_CODES { get; set; }
public string TRZ_ENDPOINT { get; set; }
public int? TRZ_ENDPORT { get; set; }
public virtual ICollection<CODES> Codes { get; set; }
public virtual ICollection<HELPER_CODES> HelperCodes { get; set; }
}
But, when I load the data on DataGrid I get more columns that I have defined on XAML. I get my columns and 13 columns more (on for each CODE_LEVEL_CONFIGURATION property).
What am I doing wrong?
Try setting AutoGenerateColumns to False
<DataGrid x:Name="LevelConfigurationDataGrid" AutoGenerateColumns="False"
Grid.Column="1" Grid.Row="1" Margin="20,0" ItemsSource="{Binding LevelConfigs}">
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>

WPF DataGridCheckColumn, check checkbox after selection changed

I have DataGrid like:
<DataGrid Name="paragonyDG" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding zaznaczone}" >
<DataGrid.Columns>
**<DataGridCheckBoxColumn Header="Zaznacz" Binding="{Binding check}"></DataGridCheckBoxColumn>**
<DataGridTextColumn Header="Nazwa paragonu" Binding="{Binding nazwa}"></DataGridTextColumn>
<DataGridTextColumn Header="Wystawiony przez" Binding="{Binding osoba}"></DataGridTextColumn>
<DataGridTextColumn Header="Kwota paragonu" Binding="{Binding kwota}"></DataGridTextColumn>
<DataGridTextColumn Header="Rabat" Binding="{Binding rabat}"></DataGridTextColumn>
<DataGridTextColumn Header="Data otwarcia" Binding="{Binding dataO}"></DataGridTextColumn>
<DataGridTextColumn Header="Data zamknięcia" Binding="{Binding dataZ}"></DataGridTextColumn>
<DataGridTextColumn Header="Formy płatności" Binding="{Binding formy}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
And its bind into class :
public class zaznaczone
{
public bool check { get; set; }
public int docID { get; set; }
public string nazwa { get; set; }
public string osoba { get; set; }
public decimal kwota { get; set; }
public decimal rabat { get; set; }
public string dataO { get; set; }
public string dataZ { get; set; }
public string formy { get; set; }
}
Now, in datagrid.selectionchanged i'd like to check this checkbox.
I've tried to parse selectedItem.columns[0] to checkbox, but i cannot parse datagridrow to "zaznaczone".
How can i check checkbox?
Specify SelectedItem property of DataGrid: SelectedItem="{Binding zaznaczoneItem}". After that you can modify check property of zaznaczoneItem.
Please note that your DataContext should implement INotifyPropertyChanged. I recommend you to look on MVVMLight framework

Why does WPF Datagrid SelectionUnit="Cell" cause disabling of DataGridTemplateColumn controls?

This seems a bit bizzare: I have a Datagrid with a button column which deletes the row when the button for that row is clicked. BUT if I set the Datagrid SelectionUnit="Cell" then the button column is disabled and I can no longer click the button.
Can anyone tell me why this happens and how to avoid the disabling behaviour for the column?
Here is XAML that recreates the issue - add & remove the SelectionUnit="Cell" to see the change in behaviour
<Window.Resources>
<local:DummyCollection x:Key="stringCollection">
<local:Dummy x="1" y="2" z="3" />
<local:Dummy x="4" y="5" z="6" />
<local:Dummy x="7" y="8" z="9" />
</local:DummyCollection>
</Window.Resources>
<StackPanel>
<DataGrid ItemsSource="{Binding Source={StaticResource stringCollection}}" AutoGenerateColumns="False" SelectionUnit="Cell">
<DataGrid.Columns>
<DataGridTextColumn x:Name="a" Binding="{Binding Path=x}" Header="a" />
<DataGridTextColumn x:Name="b" Binding="{Binding Path=y}" Header="b" />
<DataGridTextColumn x:Name="c" Binding="{Binding Path=z}" Header="c" />
<DataGridTemplateColumn x:Name="deleteButtonColumn" Header="D">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="Delete" >D</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
Add this to the Window's code behind:
public class Dummy
{
public string x { get; set; }
public string y { get; set; }
public string z { get; set; }
}
public class DummyCollection : ObservableCollection<Dummy> { }
I don't see a difference between setting SelectionUnit="Cell" or SelectionUnit="FullRow". Neither work because the command property isn't binding to a command.
You should be binding the Command property of your button to an implementation of ICommand.
Here's an example based on your code using MVVM Light's RelayCommand.
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<DataGrid ItemsSource="{Binding Items}"
AutoGenerateColumns="False" SelectionUnit="Cell">
<DataGrid.Columns>
<DataGridTextColumn x:Name="a" Binding="{Binding Path=x}" Header="a" />
<DataGridTextColumn x:Name="b" Binding="{Binding Path=y}" Header="b" />
<DataGridTextColumn x:Name="c" Binding="{Binding Path=z}" Header="c" />
<DataGridTemplateColumn x:Name="deleteButtonColumn" Header="D">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.Delete,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}">D</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
public class Dummy
{
public string x { get; set; }
public string y { get; set; }
public string z { get; set; }
public override string ToString()
{
return string.Format("x:{0}, y:{1}, z:{2}", x, y, z);
}
}
public class DummyCollection : ObservableCollection<Dummy> { }
public class ViewModel
{
public ViewModel()
{
Items = new DummyCollection
{
new Dummy {x = "1", y = "2", z = "3"},
new Dummy {x = "4", y = "5", z = "6"},
new Dummy {x = "7", y = "8", z = "9"},
};
Delete = new RelayCommand<Dummy>(DeleteItem);
}
public ICommand Delete { get; private set; }
private void DeleteItem(Dummy item)
{
Debug.WriteLine("Delete item, " + item);
}
public DummyCollection Items { get; private set; }
}

Silverlight 4 - Calling Method from DataGrid in MVVM-Pattern

I got a mvvm problem with calling methods from data grid in silverlight.
I would like to register a trigger on the property changed event for each row.
Problems:
- Binding a method to another data context (myMVVM) and not to the MyEntity object
- Get as much information as possible like dataItem, call and property and pass it to OnPropertyChanged
Any idea?
This is what i would like to have:
<Grid DataContext="{Binding myMVVM}">
<data:DataGrid ItemsSource="{Binding MyCollection}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Binding="{Binding Text}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PropertyChanged">
<i:CallMethodAction TargetObject="{Binding}" Method="OnPropertyChanged"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</data:DataGridTextColumn>
<data:DataGridTextColumn Binding="{Binding Text2}" />
<data:DataGridTextColumn Binding="{Binding Text3}" />
<data:DataGridTextColumn Binding="{Binding Text4}" />
<data:DataGridTextColumn Binding="{Binding Text5}" />
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
public class MyMVVM {
public System.Collections.Generic.List<MyEntry> MyCollection { get; set; }
public void OnPropertyChanged(object sender, MyEventArgs ea) {
DataGrid mySender = (DataGrid)sender;
MyEntry dataItem = ea.DataItem;
string propertyName = ea.PropertyName;
}
}
public class MyEntry : System.ComponentModel.INotifyPropertyChanged {
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private string _text;
public string Text {
get { return _text; }
set {
_text = value;
RaisePropertyChangedEvent("Text");
}
}
public string Text2 { get; set; }
public string Text3 { get; set; }
public string Text4 { get; set; }
public string Text5 { get; set; }
public void RaisePropertyChangedEvent(string propname) {
if (PropertyChanged != null) {
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propname));
}
}
}
In XAML, you want to respond to the PropertyChanged event at the DataGridTextColumn.
Unfortunately, the DataGridTextColumn doesn´t have events.
If you want to respond to changes in a cell, it is recommended to set the Behavior directly to the DataGrid. This provides the CellEditEnded event.
here the solution:
<UserControl.Resources>
<ViewModel:MyViewModel x:Key="myViewModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<sdk:DataGrid d:LayoutOverrides="Width" AutoGenerateColumns="False" ItemsSource="{Binding Collection}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnded">
<ei:CallMethodAction TargetObject="{StaticResource myViewModel}" MethodName="OnPropertyChanged" />
</i:EventTrigger>
</i:Interaction.Triggers>
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding Text}" Header="Text"/>
<sdk:DataGridTextColumn Binding="{Binding Text2}" Header="Text2"/>
<sdk:DataGridTextColumn Binding="{Binding Text3}" Header="Text3"/>
<sdk:DataGridTextColumn Binding="{Binding Text4}" Header="Text4"/>
<sdk:DataGridTextColumn Binding="{Binding Text5}" Header="Text5"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>

Resources