I have a DataGrid with ItemsSource bound to <ObservableCollection<MyItem>()
MyItem is a class with just two public strings. It has a default constructor (sets Text1 to something and Text2 to something (i.e "test1" and "test2").
For the DataGrid, I set CanUserAddRows to True and IsReadOnly is False.
Yet the DataGrid is not properly adding blank row. I mean it always opens with single blank row at the bottom, but this additional blank row is not set with default values - those I left in MyItem's default constructor). Also the entire row is kind of like nulled (?). I know that, because I have some XAML bindings to background / foreground cell colors and - on rows (MyItems) I added manually, before I bind the entire ObservableCollection to DataGrid) - those color-bindings work fine. On this odd new blank row, they are failing (saying that they "...can't cast MS.Internal.Namedobject...").
There is one more issue - when I start editing this weird blank row, there is never a new row added automatically at the bottom of DataGrid. Not even after pressing Enter, after in-cell text editing. In WinForms, new blank row always appeared automatically when at least one cell was touched (not even value changed, immediately after click on a blank cell).
What am I missing?
public class MyItem
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public MyItem()
{
Text1 = "test1";
Text2 = "test2";
}
}
/* ... XAML ... */
<DataGrid x:Name="dataGrid"
AutoGenerateColumns="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
CellStyle="{StaticResource dataGridCell}"
SelectionMode="Single"
SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTemplateColumn Width="1*">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Text1}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Text1}"
TextChanged="TextBox1_TextChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="1*">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Text2}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Text2}"
TextChanged="TextBox2_TextChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I found the reason. For DataGridTemplateColumn, both CellTemplate and CellEditingTemplate cannot be TextBoxes. When I changed CellTemplate to TextBlock, it started working fine.
Related
I am having problems binding to a DataGridTemplateColumn column on my control. I've read several posts about this, but none seem to be working properly for me, possibly because of the application architecture - I'm not sure. Binding to the DataGridTextColumn works, but the ComboBox I have defined inside the template column is blank. I have this XAML snippet::
<jibcontrols:JibGrid x:Name="UsersDataGrid">
...
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Text="{Binding Path=Role}" ItemsSource="{Binding Item, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}, Mode=FindAncestor}}" DisplayMemberPath="Role"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="2*" Binding="{Binding Role}"></DataGridTextColumn>
...
We are using a message bus architecture, so the data source is not ready when the page initially loads. After the data is returned from the service, a method is called to populate the DataGrid:
private void UpdateGrid(Users o)
{
Dispatcher.Invoke(() =>
{
var entities = o.OrderBy(e => e.Role);
UsersDataGrid.FilteredItemsSource = entities;
UsersDataGrid.DataContext = entities;
});
}
What have I gotten wrong in this scenario?
I looked through a tutorial and saw what I was doing wrong. There were a couple things, but how I resolved it was creating a custom class with the values I needed (there were only two):
public class RoleList : List<string>
{
public RoleList()
{
this.Add("Administrator");
this.Add("Operator");
}
}
Next, I added the namespace where the class resided:
xmlns:dm="clr-namespace:Coasters.ViewModels"
I then added a Page.Resources tag to hold my new data source:
<Page.Resources>
<dm:RoleList x:Key="RoleList" />
</Page.Resources>
Last but not least, I actually configured the ComboBox correctly (always a useful thing to do), using the ItemsSource pointing to my data source, and the SelectedItem pointing to the class property that holds the "Role" field:
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource RoleList}" SelectedItem="{Binding Role}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
This is my DataGrid;
<DataGrid Visibility="Visible"
Grid.Row="1"
SelectionUnit="CellOrRowHeader"
Name="dataGrid"
SelectionMode="Single"
ItemsSource="{Binding collcection}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy" Click="MenuItem_Click_1"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Select">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
x:Name="cbRunRobot"
IsChecked="{Binding Value}"
Width="60"
Height="25"
Checked="cbRunRobot_Checked"
Unchecked="cbRunRobot_Unchecked"
Margin="25,0,0,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
View Model:
list<MyData> collcection;
public class MyData
{
public string Name;
public string Id;
}
I try to add this Column:
<DataGridColumn Binding="{Binding Name}" Header="Name" Width="180"/>
And got this errors:
Error 1 The type "DataGridColumn" is abstract and must include an explicit value.
Error 2 The member "Binding" is not recognized or is not accessible.
DataGridColumn is an abstract class which means it cannot be instantiated. The same applies to DataGridBoundColumn.
You're choices are:
DataGridCheckBoxColumn for boolean values
DataGridComboBoxColumn for enumerable values
DataGridHyperlinkColumn for Uri values
DataGridTemplateColumn to show any types of data by defining your own cell template
DataGridTextColumn to show text values
It looks like DataGridTextColumn is what you're looking for.
Hi I can suggest you the next:
Bind to ObservableCollection instead the list.
Make your MyData model to implement InotifyPropertyChanged.
Make each binding involved property in MyData model to fire OnPropertyChanged event.
Here is the link to the working example:How to Display and select items in a Datagrid ComboBox with WPF C#, using MVVM.
regards,
I'm having trouble understanding a discrepancy in the handling of two-way binding between a DataGrid vs. a ListView. To illustrate, I have a class DataItem with a few properties, and a List of DataItems for binding to the DataGrid/ListView:
public class DataItem
{
public bool Flag { get; set; }
public int IntValue { get; set; }
public string StringValue { get; set; }
public List<DataItem> SubList { get; set; }
public DataItem()[...]
}
I create a main DataItem object with a number of additional DataItem objects adde into the SubList. The main DataItem is set to the DataContext of a containing Grid, and the SubList is bound to both a ListView:
<ListView ItemsSource="{Binding Path=SubList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Flag" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="FlagCheckBox" IsChecked="{Binding Path=Flag}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="String Value" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="StringTextBox" Text="{Binding Path=StringValue}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
and to a DataGrid:
<DataGrid ItemsSource="{Binding Path=SubList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Flag" Width="SizeToCells">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="FlagCheckBox" IsChecked="{Binding Path=Flag}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="String Value" Width="SizeToCells">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="StringTextBox" Text="{Binding Path=StringValue}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Both the ListView and the DataGrid properly display the SubList items. However, when I modify the data in the UI, and examine the source DataItem.SubList, the ListView works and the DataGrid doesn't: I can see the changes when they are made in the ListView, but when the changes are made in the DataGrid, there are no changes.
The bindings must be correct, otherwise I wouldn't see the values displayed properly. But for some reason, two-way binding works in the ListView to move changes made in the UI back to the source object, but not in the DataGrid.
You need to set UpdateSourceTrigger to PropertyChanged to propagate changes back to your DataObject class in case of DataGrid.
<CheckBox Name="FlagCheckBox" IsChecked="{Binding Path=Flag,
UpdateSourceTrigger=PropertyChanged}"/>
And on TextBox too -
<TextBox Name="StringTextBox" Text="{Binding Path=StringValue,
UpdateSourceTrigger=PropertyChanged}"/>
I am trying to create a DataGrid in WPF 4.0 using MVVM...
Features required -
Muti - Select rows using a checkbox (single click)
A select all checkbox to check all the checkboxes in the datagrid
Something like this -
It has been 2 days and i am not able to figure out how to solve the problem effectively..
A working example is what I need now ASAP..
I'll highly appreciate if someone has a working solution to share with me...
N please don't tell me to google this thing because none of the things worked out for me...
UPDATE -
I am using AutoGeneration of Columns
I don't want to add "IsSelected" or any of such property in my MODEL..
I am just facing 2 problems -
Firstly, "Select all" Feature i.e. checking all checkboxes on the checkbox click of the one present in the column header...(I am able to select and unselect the datagrid but not able to tick/untick the checkboxes)
Secondly, Multiple selection on mouse click without holding Ctrl Key..
When you're working with MVVM, you have to be aware of what is considered data and what is strictly UI.
Is your SelectedItems going to be part of your data, or only your UI?
If it's part of your data, you really should have an IsSelected property on your data model, even if that means extending the data class to include an IsSelected property, or creating a wrapper class that only contains bool IsSelected and object MyDataItem. The first option is probably preferred, since you could keep AutoGenerateColumns="True", and it makes the column bindings simpler.
Then you would just bind your DataGridRow.SelectedItem to the IsSelected property of the data item:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
But if your SelectedItems is only for the UI, or if you are breaking the MVVM pattern for some reason in this instance, than you can create the unbound CheckBox and use some code behind to ensure the CheckBox is correctly synchronized to the SelectedItem.
I did a quick sample app, and here is what my code looked like:
First off, I just added the unbound CheckBox column to the column list using a DataGridTemplateColumn. This will get added before the AutoGenerateColumns list of columns.
<DataGrid x:Name="TestDataGrid" ItemsSource="{Binding Test}"
SelectionMode="Extended" CanUserAddRows="False"
PreviewMouseLeftButtonDown="TestDataGrid_PreviewMouseLeftButtonDown_1">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="TestCheckBox"
PreviewMouseLeftButtonDown="CheckBox_PreviewMouseLeftButtonDown" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Second, I added a PreviewMouseDown event to the CheckBox to make it set the IsSelected property of the row.
private void CheckBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var chk = (CheckBox)sender;
var row = VisualTreeHelpers.FindAncestor<DataGridRow>(chk);
var newValue = !chk.IsChecked.GetValueOrDefault();
row.IsSelected = newValue;
chk.IsChecked = newValue;
// Mark event as handled so that the default
// DataGridPreviewMouseDown doesn't handle the event
e.Handled = true;
}
It needs to navigate the VisualTree to find the DataGridRow associated with the clicked CheckBox to select it, and to make life easier I am using some custom VisualTreeHelpers that I have on my blog to find the DataGridRow. You can use the same code, or you can create your own method for searching the VisualTree.
And last of all, if the user clicks on anywhere other than the CheckBox, we want to disable the default DataGrid selection event. This ensures that the IsSelected value will only change when you click on the CheckBox.
There are multiple ways of doing this that will disable the selection at different levels, but to make life simple I just disabled the DataGrid.PreviewMouseLeftButtonDown event if the user didn't click on the CheckBox.
private void TestDataGrid_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
var chk = VisualTreeHelpers.FindAncestor<CheckBox>((DependencyObject)e.OriginalSource, "TestCheckBox");
if (chk == null)
e.Handled = true;
}
I using my custom VisualTreeHelpers again to navigate the visual tree and find out if the CheckBox was clicked on, and cancelling the event if the user clicked on anywhere other than the CheckBox.
As for your 2nd request of adding a CheckBox to SelectAll or UnselectAll items, this would once again be dependent on if your selection is part of the UI or the data.
If it's part of the UI, simply add a CheckBox to the DataGridTemplateColumn.HeaderTemplate, and when it's clicked, loop through the DataGrid.Rows, find the CheckBox in the first column, and check or uncheck it.
If it's part of the data you could still do the same thing (only set the bound value in the DataGrid.Items instead of the CheckBox.IsChecked from the DataGrid.Rows), or you could do as Adolfo Perez suggested, and bind it to a property on the ViewModel.
For an MVVM Solution you could try this:
<StackPanel>
<DataGrid ItemsSource="{Binding Path=TestItems}" AutoGenerateColumns="False" Name="MyDataGrid"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" Width="50" >
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll" Content="All" IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="Name" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
In your ViewModel:
private void PopulateTestItems()
{
TestItems = new ObservableCollection<TestItem>();
for (int i = 0; i < 5; i++)
{
TestItem ti = new TestItem();
ti.Name = "TestItem" + i;
ti.IsSelected = true;
TestItems.Add(ti);
}
}
private bool _AllSelected;
public bool AllSelected
{
get { return _AllSelected; }
set
{
_AllSelected = value;
TestItems.ToList().ForEach(x => x.IsSelected = value);
NotifyPropertyChanged(m => m.AllSelected);
}
}
private ObservableCollection<TestItem> _TestItems;
public ObservableCollection<TestItem> TestItems
{
get { return _TestItems; }
set
{
_TestItems = value;
NotifyPropertyChanged(m => m.TestItems);
}
}
And finally the sample Model class:
public class TestItem : ModelBase<TestItem>
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
NotifyPropertyChanged(m => m.Name);
}
}
private bool _IsSelected;
public bool IsSelected
{
get { return _IsSelected; }
set
{
_IsSelected = value;
NotifyPropertyChanged(m => m.IsSelected);
}
}
}
Most of the code above should be self-explanatory but if you have any question let me know
Your view can be something like
<DataGrid Name="SomeDataGrid" Grid.Row="0" ItemsSource="{Binding Path=SomeCollection}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.AllItemsAreChecked}" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<CheckBox Focusable="False" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="RandomNumber" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<TextBlock Text="{Binding Path=RandomNumber}" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Date" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<TextBlock Text="{Binding Path=Date}" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Time" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<TextBlock Text="{Binding Time}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And in viewmodel
SomeCollection binding property is an observablecollection
sometype contains properties like IsSelected , RandomNumber ,Date , Time
for eg:
class ViewModel
{
public ObservableCollection<SomeType> SomeCollection{get;set;}
}
class SomeType
{
public string Date {get;set;}
public string Time {get;set;}
public string RandomNumber {get;set;}
public bool IsSelected {get;set;}
}
Bear with me if you would, I'm brand new to WPF MVVM development.
The utility I'm working on (WPF client application) contains a 4 column datagrid that displays the currently available data to the user for a specified period of time. The records returned may contain all or just some of the data ultimately required for the record to be complete. The user should be able to modify the missing pieces or add new rows altogether to fill in missing records. 3 of the 4 columns contain combo boxes with only valid choices. The combo boxes are cascading so that the first combo determines the contents of combo 2 and 3. The 4th column is a simple price column.
I've managed to get the grid working perfectly with the default return data. The combox populate correctly as well as display the current record's value as the selected item in the combo.
My problem is with adding a new row. I've been trying to accomplish this inline. Meaning the user clicks on or tabs to the blank row at the bottom of the list and a new row gets added to collection accepts input. The first combo is populated in the empty row, but selecting a value does not cascade the changes to the other 2 combos. It works up in the populated rows though.
That make sense? I'm probably missing something so stupid simple that I'll kick myself when someone points it out but right now I'm tearing out what little hair I have left.
So, Is it possible to have a new user add a row just by tabbing through to the next line?
Do I need to anything special to wire up the cascading combo boxes in the new row?
Thanks!
Here's my XAML (essentially pseudo code after stripping it down so don't mind mispellings):
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="1024">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<StackPanel>
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding Prices}"
SelectedItem="{Binding SelectedPrice}"
CanUserAddRows="True">
<DataGrid.Columns>
<!-- Price Group -->
<DataGridTemplateColumn MinWidth="120"
Header="Price Group">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cboPriceGroup"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.PriceGroups}"
DisplayMemberPath="Name"
SelectedItem="{Binding PriceGroupObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Option 1 -->
<DataGridTemplateColumn MinWidth="120"
Header="Option1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cboOption1"
ItemsSource="{Binding PriceGroupObject.Options1}"
DisplayMemberPath="Code"
SelectedValuePath="Id"
SelectedValue="{Binding Option1Id, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Option 2 -->
<DataGridTemplateColumn MinWidth="120"
Header="Option2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cboOption2"
ItemsSource="{Binding PriceGroupObject.Options2}"
DisplayMemberPath="Code"
SelectedValuePath="Id"
SelectedValue="{Binding Option2Id, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Price -->
<DataGridTemplateColumn MinWidth="120"
Header="Price">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtPrice"
Text="{Binding Price, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
And here's the ViewModel:
// ViewModelBase implements INotifyPropertyChanged
public class MainWindowViewModel : ViewModelBase
{
#region Declarations
// Comes From Backend.Services
private IPriceListService _PriceListService;
private ObservableCollection<Price> _Prices;
private Price _SelectedPrice;
private IEnumerable<PriceGroup> _PriceGroups;
private PriceGroup _SelectedPriceGroup;
// Same for Options1, Options2
#endregion
#region Properties
public ObservableCollection<Price> Prices
{
get { return _Prices; }
set
{
if (_Prices != value)
{
_Prices = value;
OnPropertyChanged("Prices");
}
}
}
public Price SelectedPrice
{
get { return _Price; }
set
{
if (_Price != value)
{
_Price = value;
OnPropertyChanged("SelectedPrice");
}
}
}
// Same for PriceGroups, Options1, Options2
#endregion
#region Constructor
public MainViewModel()
: this(new FakePriceListService())
{
}
// Constructor
public MainViewModel(IPriceListService priceListService)
{
_PriceListService = priceListService;
InitializeViewModel();
}
protected override void InitializeViewModel()
{
// Test ID
LoadFuturesPrices(12345);
LoadPriceGroups();
base.InitializeViewModel();
}
#endregion
#region Methods
void LoadFuturesPrices(short FinancialPeriodId)
{
Prices = _PriceListService.GetFuturesPriceList(FinancialPeriodId);
}
void LoadPriceGroups()
{
PriceGroups = _PriceListService.GetPriceGroups();
}
void PriceGroupSelected(string PriceGroupId)
{
SelectedPriceGroup = (from p in PriceGroups where p.Name == PriceGroupId select p).SingleOrDefault();
}
#endregion
}