I have this class
public class Doctor
{
private string licenseNumber;
public string LicenseNumber
{
get
{
return this.licenseNumber;
}
set
{
if (value.Length > 4)
throw new MyException("License Number can't be more then 4 digits.\n");
if (!new Regex("^[0-9]*$").Match(value).Success) // Allow only numbers
throw new MyException("Only numbers are allowed in a License Number.\n");
this.licenseNumber = value.PadLeft(4,'0'); // Pad with zeros to 4 digits
}
}
private string name;
public string Name
{
get
{
return this.name;
}
set
{
if (!new Regex("^[a-zA-Z ]*$").Match(value).Success) // Allow only letters and spaces
throw new MyException("Only letters and spaces are allowed in a name.\n");
this.name = value;
}
}
private string phoneNumber;
public string PhoneNumber
{
get
{
return this.phoneNumber;
}
set
{
{
if (!new Regex("^0[2-9]-[0-9]{7}$").Match(value).Success) // allow only numbers in format 0[2-9]-xxxxxxx
throw new MyException("ERROR: only numbers in format 0[2-9]-xxxxxxx are legal\n");
this.phoneNumber = value;
}
}
}
}
and much more properties inside it.
Of course the ctor of this class accepts only form of ctor with values inside, and enter the values to the props.
Now, I've created a form, which within the user can add a new Doctor.
Thing is - I want to validate the form before the user click on 'Add'.
This is a shorted version of my 'add' form in XML:
<Window x:Class="PLForms.AddDoctor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AddDoctor" Height="431" Width="381" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:BE;assembly=BE" Loaded="Window_Loaded" Background="White" ResizeMode="CanResizeWithGrip" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="12" HorizontalAlignment="Left" VerticalAlignment="Top">
<Window.Resources>
<CollectionViewSource x:Key="doctorViewSource" d:DesignSource="{d:DesignInstance my:Doctor, CreateList=True}" />
</Window.Resources>
<Grid Height="367" Name="grid1" Width="296" Margin="12" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="29" />
</Grid.RowDefinitions>
<Grid DataContext="{StaticResource doctorViewSource}" HorizontalAlignment="Left" Margin="19,13,0,0" Name="grid2" VerticalAlignment="Top">
<Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="nameTextBox" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
<Label Content="Phone Number:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="3" Name="phoneNumberTextBox" Text="{Binding Path=PhoneNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
</Grid>
<Button Content="Add" Name="Add" BorderBrush="#FF612355" Foreground="White" Click="Add_Click" Margin="0,326,0,0" Grid.RowSpan="2">
<Button.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#85081DDA" Offset="0.893" />
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
Now, I think that the validation of data will activated only if I set "DataContext" somehow. I've tried to set a sample Doctor object in the form, assign DataContext to the instance of this object, and then - when the user update the data to illegal one - it activate the validation, and red border is marked around the textbox.
But I'm looking how to 'activate' validation even if there's no instance of Doctor before - when the user got a blank form, fill it by himself, and only then try to click on 'Add'.
My problem is that in this way, I can't attach DataContext property to anything, since I don't have a Doctor instance, and of course I can't create an empty one, because all of the exceptions I created myself...
I'll be happy to know what's the logic of Adding data to database, and validate it before, not just in update.
T
Have you looked at validation attributes? That should make your code cleaner and you don't have to throw exceptions anymore since the user won't be able to add invalid data to the database.
Related
I have a UserControl that is a portion of a wpf window.
<Window>
<Grid>
<!--some other display elements would be here-->
<local:MyUserControl x:Name="Foo" Padding="0,42,0,50"/>
</Grid>
</Window>
Inside MyUserControl I have an element that is a gallery that is normally hidden, but when visible, it should fill the entire screen.
<UserControl>
<Grid>
<!--main display elements would be here-->
<Grid Name="Gallery" Visibility="Hidden">
<Rectangle Fill="Black" Opacity="0.75"/>
<TextBlock Name="GalleryLabel" Foreground="White" TextAlignment="Center">Current Image Title</TextBlock>
<Button Name="CloseGallery" Style="{DynamicResource WhiteTextButton}" Margin="0,0,0,0" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Width="25" Click="GalleryClose_OnClick">X</Button>
<Image Name="GalleryImage" Margin="25"/>
</Grid>
</Grid>
</UserControl>
How can I set the Gallery to fill the entire Window rather than just the UserControl?
I was able to get it to work by adding Margin="0,-42,0,-50" to Gallery, but I don't like that solution. I would rather do something that doesn't involve hard-coding values in the UserControl so that I would be able to have more flexiblility in using it.
Normally it looks like:
where the green Foo area is MyUserControl, and the rest of the things in the window are other elements.
At certain points, I have a gallery display an image, which should fill the entire screen like:
which should fill the entire screen and have a black opaque overlay, along with an image displayed on top of the overlay.
Remove the Padding...
You can use:
<local:MyUserControl x:Name="Foo" VerticalAlignment="Stretch" HorizontalAligment="Stretch"/>
EDIT
I may admit that I still am not sure what you want. But I made something which does the same as you showed in the pictures. But be aware that this as an opinion based question, and the answer is my opinion, there are a lot of ways to achieve this. And this is only one of them:
MainWindow
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myProject="clr-namespace:MyProject"
Title="MainWindow" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Background="Gainsboro" Grid.Row="0">
<StackPanel Orientation="Horizontal">
<Button Content="Show gallery" Width="100" Margin="10"/>
<Button Content="Do something else" Width="150" Margin="10"/>
</StackPanel>
</Border>
<myProject:SomeOtherStuff Grid.Row="1" />
<Border Grid.Row="2" Background="Gainsboro">
<StackPanel Orientation="Horizontal">
<Label Content="Buttom area, can be used for something else"/>
</StackPanel>
</Border>
<myProject:GalleryUserControl x:Name="GalleryUserControl" Grid.Row="0" Grid.RowSpan="3" Visibility="Hidden"/>
</Grid>
SomeOtherStuff UserControl
<UserControl x:Class="MyProject.SomeOtherStuff"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="Green">
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Content="Foo" FontSize="30" FontFamily="Verdana"/>
</Grid>
Gallery UserControl
<UserControl x:Class="XamDataGrid.GalleryUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="500" Width="800">
<Grid Background="#99000000">
<TextBlock Text="Currect image title" Foreground="White" TextAlignment="Center" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<Button Name="CloseGallery" Margin="0,0,0,0" Content="X" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Width="25" Click="GalleryClose_OnClick"/>
<Image Margin="30"/>
</Grid>
An Adorner will be best for you. As it floats above everything else and remains outside the VisualTree too.
UserControl2
<UserControl2 ...>
<Grid>
<Grid Background="#FF709FA6" Opacity="0.3" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}">
</Grid>
<Grid Width="600" Height="700">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF37DAEA" Offset="0"/>
<GradientStop Color="#FFE84242" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<!-- Gallery controls -->
<Button Content="Welcome to Gallery" HorizontalAlignment="Left" IsHitTestVisible="True" Margin="112,126,0,0" VerticalAlignment="Top" Height="68" FontSize="48" Click="Button_Click_1"/>
<TextBlock HorizontalAlignment="Left" Margin="83,42,0,0" TextWrapping="Wrap" Text="UserControl2" VerticalAlignment="Top" Foreground="#FF1B0D0D"/>
</Grid>
</Grid>
</UserControl2>
UserControl1
Code :
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// get root Window
DependencyObject current = LogicalTreeHelper.GetParent(this);
while (!(current is Window))
current = LogicalTreeHelper.GetParent(current);
Window root = current as Window;
IEnumerable children = LogicalTreeHelper.GetChildren(root);
Panel p = null;
foreach (var child in children)
{
p = child as Panel; // get first container panel in Window
break;
}
// Create UserControl2 to add to the adorner
UserControl2 ctrlGallery = new UserControl2();
AdornerLayer layer = AdornerLayer.GetAdornerLayer(p);
GalleryAdorner adorner = new GalleryAdorner(p, ctrlGallery);
layer.Add(adorner);
}
}
public class GalleryAdorner : Adorner
{
private Control _child;
VisualCollection collection;
public GalleryAdorner(UIElement elem, Control child)
: base(elem)
{
collection = new VisualCollection(this);
_child = child;
collection.Add(_child);
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Visual GetVisualChild(int index)
{
if (index != 0) throw new ArgumentOutOfRangeException();
return collection[0];
}
protected override Size MeasureOverride(Size constraint)
{
_child.Measure(constraint);
return _child.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
_child.Arrange(new Rect(new Point(0, 0), finalSize));
return new Size(_child.ActualWidth, _child.ActualHeight);
}
}
i,m try to learning wpf application. i try to use ninject for DI in WPF, and work perfectly. and then, i try to create login form. in this part i have some problem, i can't bind data from view to VM when the button clicked.
this my code
LoginUserControl
<UserControl x:Class="Middleware_v2._0_with_Modern_Ui.UserControls.LoginUserControl"
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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:vm="clr-namespace:Middleware_v2._0_with_Modern_Ui.ViewModel"
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
mc:Ignorable="d" DataContext="{Binding Login, Source={StaticResource Locator}}">
<Border Width="400" Height="300" BorderBrush="LightBlue" CornerRadius="5" Background="SkyBlue" Margin="0,-100,0,0">
<StackPanel Margin="0,0,10,0" Width="400">
<Label VerticalAlignment="Center" HorizontalAlignment="Center"
Content="Login Form" Margin="0,30,0,0" FontSize="20" FontWeight="Bold"/>
<Label Content="User Name" Margin="84,45,197,0"/>
<TextBox Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="150" Height="25" Margin="165,-20,85,0"/>
<Label Content="Password" Margin="84,15,197,0"/>
<PasswordBox x:Name="txt_password" Width="150" Height="25" Margin="165,-20,85,0"/>
<Button Width="100" Height="30" Margin="-110,20,0,0" Content="Login" />
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand x:Name="btnClicked" Command="{Binding Authorize, Mode=OneTime}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
<!--<Button Width="100" Height="30" Margin="110,-30,0,200" Content="Exit" />-->
</StackPanel>
<Border.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="15" Color="Gray" Direction="10"/>
</Border.Effect>
</Border>
LoginViewModel
public class LoginViewModel : ViewModelBase, ILoginViewModel
{
private readonly IAccountService _accountService;
public LoginViewModel(IAccountService _accountService)
{
this._accountService = _accountService;
Authorize = new RelayCommand(() => CheckAuthorized(), () => true);
}
public RelayCommand Authorize { get; set; }
private void CheckAuthorized()
{
User newUser = new User();
newUser.LoginName = _username;
newUser.LoginPassword = _password;
User user = _accountService.AuthenticationUser(newUser, 1);
throw new System.NotImplementedException();
}
}
LoginViewModel Interface
public interface ILoginViewModel
{
RelayCommand Authorize { get; set; }
}
how, to solve this? can some one help me
Simply
<Button Width="100" Height="30" Margin="-110,20,0,0" Content="Login" Command="{Binding Authorize}" />
I'm beginner in WPF development.
I create some window with frame inside.
I wrote a template for the frame and i`m trying to bind to the buttons inside the freame template commands without success.
see the next code:
<Grid.Resources>
<ControlTemplate TargetType="Frame" x:Key="NavigationButtonsTemplates">
<DockPanel>
<StackPanel
Margin="7"
Orientation="Horizontal"
DockPanel.Dock="Bottom"
HorizontalAlignment="Right"
>
<Button
Content="Back"
Command="{x:Static NavigationCommands.BrowseBack}"
Margin="5,0,5,0" Width="80" />
<Button
Content="Next"
Command="{Binding Path=NavigateToPersonalDataCommand}"
Margin="5,0,5,0" Width="80"></Button>
</StackPanel>
<Border
BorderBrush="LightBlue"
Margin="7,8,9,0"
BorderThickness="7"
Padding="5"
CornerRadius="7"
Background="White"
>
<ContentPresenter />
</Border>
</DockPanel>
</ControlTemplate>
</Grid.Resources>
It seems that the binding of the buttons is not working.
When i put the buttons outside the tag it`s work perfectly.
How can I bind a command to a button which located in ?
Thanks
Your ControlTemplate needs to have a DataContext. Try changing the DockPanel element to bind to the DataContext property on the templated frame. This assumes that the Frame that is being templated has a valid DataContext.
<DockPanel DataContext="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}">
or
<DockPanel DataContext="{TemplateBinding DataContext}">
Edit:
After trying to run your code I found the problem. The properties you are binding to in XAML do not match the properties on your view model.
Changing your XAML to bind to the property names on your view model fixes the problem.
From this:
Command="{Binding Path=NavigateNext}"
Command="{Binding Path=NavigateToPersonalData}"
To this:
Command="{Binding Path=BrowseNext}"
Command="{Binding Path=NavigateToPersonalDataCommand}"
to match your view model properties:
public ICommand BrowseNext
{
get
{
return m_BrowseNext;
}
set
{
m_BrowseNext = value;
}
}
public ICommand NavigateToPersonalDataCommand
{
get
{
return m_PersonalDataCommand;
}
set
{
m_PersonalDataCommand = value;
}
}
This is the Xaml code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestProj"
x:Class="TestProj.MainView"
x:Name="Window"
Title="MainView"
Width="640" Height="480">
<Window.DataContext>
<local:NavigationViewModel/>
</Window.DataContext>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.20*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<ControlTemplate TargetType="Frame" x:Key="NavigationButtonsTemplates">
<DockPanel DataContext="{TemplateBinding DataContext}">
<StackPanel
Margin="7"
Orientation="Horizontal"
DockPanel.Dock="Bottom"
HorizontalAlignment="Right"
>
<Button
Content="Back"
Command="{Binding Path=NavigateNext}"
Margin="5,0,5,0" Width="80" />
<Button
Content="Next"
Command="{Binding Path=NavigateToPersonalData}"
Margin="5,0,5,0" Width="80"></Button>
</StackPanel>
<Border
BorderBrush="LightBlue"
Margin="7,8,9,0"
BorderThickness="7"
Padding="5"
CornerRadius="7"
Background="White"
>
<ContentPresenter />
</Border>
</DockPanel>
</ControlTemplate>
</Grid.Resources>
<Button Content="Opening" Grid.Row="0"></Button>
<Button Content="Personal Data" Grid.Row="1"></Button>
<Button Content="Business Data" Grid.Row="2"> </Button>
<Button Content="Summery Report" Grid.Row="3"></Button>
<DockPanel Grid.Column="2" Grid.RowSpan="4">
<Frame x:Name="mainFrame" Template="{StaticResource NavigationButtonsTemplates}"/>
</DockPanel>
</Grid>
</Window>
Here how i wrote the commands:
class NavigationViewModel
{
private ICommand m_BrowseNext;
public ICommand BrowseNext
{
get
{
return m_BrowseNext;
}
set
{
m_BrowseNext = value;
}
}
private ICommand m_PersonalDataCommand;
public ICommand NavigateToPersonalDataCommand
{
get
{
return m_PersonalDataCommand;
}
set
{
m_PersonalDataCommand = value;
}
}
public NavigationViewModel()
{
BrowseNext = new RelayCommand(new Action<object>(NavigateNext));
NavigateToPersonalDataCommand = new RelayCommand(new Action<object>(NavigateToPersonalData));
}
public void NavigateToPersonalData(object obj)
{
MainView.Instance.GetMainFrame.Navigate(Pages.Opening.Instance);
}
public void NavigateNext(object obj)
{
MainView.Instance.GetMainFrame.Navigate(Pages.PersonalData.Instance);
}
}
When i move the buttons outside the the commands works great so i think the problem is in the XAML.
Thanks!!!
I am learning to use listBox in WPF with dataTemplate using the examples from MSDN, I can render a listBox bound to an ObservableCollection as a source and by overriding the ToString method.
However, I need to render an image and some texblocks for every item. Here's my XAML:
<Grid x:Class="MyAddin.WPFControls"
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:c="clr-namespace:MyAddin"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Background="Transparent"
HorizontalAlignment="Stretch" Width="auto"
Height="215" VerticalAlignment="Stretch" ShowGridLines="False">
<Grid.Resources>
<c:People x:Key="MyFriends"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Left"
IsManipulationEnabled="True"
Height="20" Width="300">Activity Feed</TextBlock>
<ListBox Grid.Row="1" Name="listBox1" IsSynchronizedWithCurrentItem="True"
BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Auto"
VerticalContentAlignment="Stretch" Margin="0,0,0,5" Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Margin="5" BorderBrush="Black" BorderThickness="1">
<Image Source="{Binding Path=Avatar}" Stretch="Fill" Width="50" Height="50" />
</Border>
<StackPanel Grid.Column="1" Margin="5">
<StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" >
<TextBlock Text="{Binding Path=Firstname }" />
</StackPanel>
<TextBlock Text="{Binding Path=Comment}" />
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
My Collection class is as following:
public class People : ObservableCollection<Person>
{ }
public class Person
{
private string firstName;
private string comment;
private Bitmap avatar;
public Person(string first, string comment, Bitmap avatar)
{
this.firstName = first;
this.comment = comment;
this.avatar = avatar;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string Comment
{
get { return comment; }
set { comment = value; }
}
public Bitmap Avatar
{
get { return avatar;}
set { avatar = value; }
}
public override string ToString()
{
return firstName.ToString();
}
}
Once my addin is loaded, I am downloading my data and setting the itemsSource.
People p = new People();
p.Add(new Person("Willa", "Some Comment", myAvatar));
p.Add(new Person("Isak", "Some Comment", myAvatar));
p.Add(new Person("Victor", "Some Comment", myAvatar));
this.wpfControl.listBox1.ItemsSource = p;
The problem I am facing is that the items are being rendered as empty rows whereas If I remove the dataTemplate, the items are rendered fine with their firstName.
Don't see anything wrong with the bindings themselves, but your avatar type seems off, WPF expects ImageSource (i do not know if there is any implicit convertion between Bitmap and ImageSource, check for binding errors to find out).
HI,
I have a list of coursesTaken with dates (returned by a nested class). Since I am not sure howmany courses the person took, I just bind it to textboxes and is working fine. The datacontext of the grid is the list itself so I assume whenever I make changes to the form, it is automatically sent back to the list and add it, but when it is time to save it in DB, the list won't change. I tried making the list as an observable collection and bind mode two way but still won't change. when I try to put Onpropertychange it says "Cannot acces a non static member of outer type via nested type..." What I want to do is whenever I type / add something to textbox, the list will add it as it's nth item so I can iterate and save it in DB.
is ther any other way to do this? please see code below. thanks!
#region class for binding purposes (nested class)
public class ListCon
{
public ObservableCollection<tblGALContinuingEducationHistory> EducList
{
get
{
return new ObservableCollection<tblGALContinuingEducationHistory>(contEducHstoryList);
}
}
}
#endregion
#region constructor
public ContinuingEducHistoryPopUp(tblAttorneyGalFileMaint currentGal)
{
contEducHstoryList = new ObservableCollection<tblGALContinuingEducationHistory>();
InitializeComponent();
if (currentGal != null)
{
CurrentGal = currentGal;
txtMemberName.Text = CurrentGal.FullName;
contEducHstoryList = new ObservableCollection<tblGALContinuingEducationHistory>(currentGal.tblGALContinuingEducationHistories.Count > 0 && currentGal.tblGALContinuingEducationHistories != null ? currentGal.tblGALContinuingEducationHistories : null);
}
this.DataContext = new ListCon();
}
#endregion
Now here's my xaml
<Grid Grid.Row="2" Grid.ColumnSpan="2" Name="grdEducForm">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Path=EducList[0].CourseTitle}" Grid.Column="0" Grid.Row="0" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[0].CertificateDate}" Grid.Column="1" Grid.Row="0" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[0].CertificateExpiration}" Grid.Column="2" Grid.Row="0" />
<TextBox Text="{Binding EducList[1].CourseTitle}" Grid.Column="0" Grid.Row="1" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[1].CertificateDate}" Grid.Column="1" Grid.Row="1" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[1].CertificateExpiration}" Grid.Column="2" Grid.Row="1" />
<TextBox Text="{Binding Path=EducList[2].CourseTitle}" Grid.Column="0" Grid.Row="2" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[2].CertificateDate}" Grid.Column="1" Grid.Row="2" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[2].CertificateExpiration}" x:Name="dpEnd3"
Grid.Column="2" Grid.Row="2" />
<TextBox Text="{Binding Path=EducList[3].CourseTitle, Mode=TwoWay}" Grid.Column="0" Grid.Row="3" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[3].CertificateDate, Mode=TwoWay}" Grid.Column="1"
Grid.Row="3" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[3].CertificateExpiration}" Grid.Column="2" Grid.Row="3" />
<TextBox Text="{Binding Path=EducList[4].CourseTitle}" Name="txtEducName5" Grid.Column="0" Grid.Row="4" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[4].CertificateDate}" x:Name="dpStart5" Grid.Column="1"
Grid.Row="4" />
<useable:MaskedDatePicker DateValue="{Binding Path=EducList[4].CertificateExpiration}" x:Name="dpEnd5"
Grid.Column="2" Grid.Row="4" />
</Grid>
In your ListCon property you always return a new collection this defeats the point of having an ObservableCollection at all, you should store it in a field like this:
//Initialize field in constructor
private readonly ObservableCollection<tblGALContinuingEducationHistory> _ListCon;
public ObservableCollection<tblGALContinuingEducationHistory> ListCon
{
get
{
return _ListCon;
}
}
EducList = new ObservableCollection<tblGALContinuingEducationHistory>(currentGal.tblGALContinuingEducationHistories.Count > 0 && currentGal.tblGALContinuingEducationHistories != null ? currentGal.tblGALContinuingEducationHistories : null);
int count = 0;
//5 is the maximum no of course an atty can take and save in this form
count = 5 - EducList.Count;
for (int i = 0; i < count; i++)
{
galContEdu = FileMaintenanceBusiness.Instance.Create<tblGALContinuingEducationHistory>();
galContEdu.AttorneyID = currentGal.AttorneyID;
EducList.Add(galContEdu);
}
then save only the ones that has data:
foreach (var item in EducList)
{
if(!string.IsNullOrEmpty(item.CourseTitle) || !string.IsNullOrWhiteSpace(item.CourseTitle))
FileMaintenanceBusiness.Instance.SaveChanges(item, true);
}