I have a form window I show with a ShowDialog()
, the textboxes are bound to properties in the view model.
I open my dialog like this (simplified version):
FilterWindowView wnd = new FilterWindowView();
FilterWindowViewModel fvm = new FilterWindowViewModel(licenseRecords) { wnd = wnd };
wnd.DataContext = fvm;
fvm.RestoreCurrentFilters();
if (wnd.ShowDialog() ?? false)
{
//...
}
The properties I set in my form are used as filter parameters, which I store in a static class to retrieve for later usage.
What I would want to do, is to have the textboxes autofill with the current value stored in this static class.
My textbox bound properties look like this:
private string _product;
public string product
{
get { return _product; }
set
{
if (_product == value)
return;
_product = value;
Helper.product = value;
if (value != "")
chkProduct = true;
OnPropertyChanged();
}
}
(I think it may be better performance wise to reassign when validating but this is another question...)
My problem here is that if I set a value (i.e. in the constructor), the value gets set but when calling ShowDialog()
, the value is reset to "".
Also tried calling a method after instantiating the VM, but as said, this reset happens when showing the window ( when calling ShowDialog()
)...
This form generates a custom object I recover in the VM dialogResult
so going wnd.Show()
and then setting to stored values is not an option for me (I guess?).
Thanks for any help.
EDIT View is as simple as it gets, just a few labels and textboxes two way bound to the VM.
<Window x:Class="LicenseManager.View.FilterWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LicenseManager.View"
mc:Ignorable="d"
Title="FilterWindowView" Height="306.412" Width="284.216">
<Grid>
<CheckBox Content="Product" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" IsChecked="{Binding chkProduct}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,8,0,0" TextWrapping="Wrap" Text="{Binding product, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Client" HorizontalAlignment="Left" Margin="10,38,0,0" VerticalAlignment="Top" IsChecked="{Binding chkClient}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,36,0,0" TextWrapping="Wrap" Text="{Binding client, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Date After" HorizontalAlignment="Left" Margin="10,66,0,0" VerticalAlignment="Top" IsChecked="{Binding chkDateAfter}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,64,0,0" TextWrapping="Wrap" Text="{Binding dateAfter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Date Before" HorizontalAlignment="Left" Margin="10,94,0,0" VerticalAlignment="Top" IsChecked="{Binding chkDateBefore}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,92,0,0" TextWrapping="Wrap" Text="{Binding dateBefore, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Sbs__no" HorizontalAlignment="Left" Margin="10,122,0,0" VerticalAlignment="Top" IsChecked="{Binding chkSbsNo}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,120,0,0" TextWrapping="Wrap" Text="{Binding sbsNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Store__no" HorizontalAlignment="Left" Margin="10,150,0,0" VerticalAlignment="Top" IsChecked="{Binding chkStoreNo}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,148,0,0" TextWrapping="Wrap" Text="{Binding storeNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Workstation__no" HorizontalAlignment="Left" Margin="10,178,0,0" VerticalAlignment="Top" IsChecked="{Binding chkWorkstationNo}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,176,0,0" TextWrapping="Wrap" Text="{Binding workstationNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="Comment" HorizontalAlignment="Left" Margin="10,206,0,0" VerticalAlignment="Top" IsChecked="{Binding chkWorkstationNo}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="137,204,0,0" TextWrapping="Wrap" Text="{Binding comment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<Button Content="Apply" Command="{Binding apply}" HorizontalAlignment="Left" Margin="10,236,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
EDIT The View Model class Helper is my static class
class FilterWindowViewModel : INotifyPropertyChanged
{
#region Attributes
public Window wnd; // For dialog closer
public List<LicenseRecordModel> list;
public List<LicenseRecordModel> dialogResult;
public event PropertyChangedEventHandler PropertyChanged;
string tmpProduct;
string tmpClient;
string tmpDateAfter;
string tmpDateBefore;
string tmpSbsNo;
string tmpStoreNo;
string tmpWorkstationNo;
string tmpComment;
#endregion
#region Properties
//Properties and commands
private string _comment;
public string comment
{
get { return _comment; }
set
{
if (_comment == value)
return;
_comment = value;
Helper.comment = value;
if (value != "")
chkComment = true;
OnPropertyChanged();
}
}
//...
private DelegateCommand _apply;
public DelegateCommand apply
{
get
{
return _apply ?? (_apply = new DelegateCommand(o => Apply(), o => true));
}
}
#endregion
#region Init
public FilterWindowViewModel(IEnumerable<LicenseRecordModel> source)
{
tmpProduct = Helper.product;
tmpClient = Helper.client;
tmpDateAfter = Helper.dateAfter;
tmpDateBefore= Helper.dateBefore;
tmpSbsNo = Helper.sbsNo;
tmpStoreNo = Helper.storeNo;
tmpWorkstationNo = Helper.workstationNo;
tmpComment = Helper.comment;
list = new List<LicenseRecordModel>(source);
}
public void RestoreCurrentFilters()
{
product = tmpProduct;
client = tmpClient;
dateAfter = tmpDateAfter;
dateBefore = tmpDateBefore;
sbsNo = tmpSbsNo;
storeNo = tmpStoreNo;
workstationNo = tmpWorkstationNo;
comment = tmpComment;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private bool Accept(LicenseRecordModel lic)
{
var tmp = list;
tmp = list.Where(x =>
chkProduct ? x.Product.Contains(product) : true &&
chkClient ? x.Client.Contains(client) : true &&
chkProduct ? x.Product.Contains(product) : true &&
chkProduct ? x.Product.Contains(product) : true &&
chkProduct ? x.Product.Contains(product) : true &&
chkProduct ? x.Product.Contains(product) : true &&
chkProduct ? x.Product.Contains(product) : true
).ToList();
return false;
}
#region Commands
public void Apply()
{
var tmp = new List<LicenseRecordModel>(list);
dialogResult = new List<LicenseRecordModel>(list);
string message = "";
if (chkProduct)
{
dialogResult =tmp.Where(x => x.Product.Contains(product.ToUpper())).ToList();
tmp = dialogResult;
}
if (chkClient)
{
dialogResult = tmp.Where(x => x.Client.Contains(client.ToUpper())).ToList();
tmp = dialogResult;
}
if (chkDateAfter)
{
DateTime after;
if (chkDateBefore)
{
DateTime before;
if (DateTime.TryParse(dateAfter, out after))
{
if (DateTime.TryParse(dateBefore, out before))
{
dialogResult = tmp.Where(x => DateTime.ParseExact(x.CreationDate, "yyyy-MM-dd", null) <= after && DateTime.ParseExact(x.CreationDate, "yyyy-MM-dd", null) >= before).ToList(); ;
tmp = dialogResult;
}
else message += "'Date Before' is not a valid date (yyyy-mm-dd)";
}
else message += "'Date After' is not a valid date (yyyy-mm-dd)";
}
else if (DateTime.TryParse(dateAfter, out after))
{
dialogResult = tmp.Where(x => DateTime.ParseExact(x.CreationDate, "yyyy-MM-dd", null) >= after).ToList();
tmp = dialogResult;
}
else message += "'Date After' is not a valid date (yyyy-mm-dd)";
}
if (chkDateBefore)
{
DateTime before;
if (DateTime.TryParse(dateBefore, out before))
{
dialogResult = tmp.Where(x => DateTime.ParseExact(x.CreationDate, "yyyy-MM-dd", null) <= before).ToList();
tmp = dialogResult;
}
else message += "'Date After' is not a valid date (yyyy-mm-dd)";
}
if (chkSbsNo)
{
dialogResult = tmp.Where(x => x.SbsNo.Contains(sbsNo)).ToList();
tmp = dialogResult;
}
if (chkStoreNo)
{
dialogResult = tmp.Where(x => x.StoreNo.Contains(storeNo)).ToList();
tmp = dialogResult;
}
if (chkWorkstationNo)
{
dialogResult = tmp.Where(x => x.WorkstationNo.Contains(workstationNo)).ToList();
tmp = dialogResult;
}
if (chkComment)
{
dialogResult = tmp.Where(x => x.Comment.ToUpper().Contains(comment.ToUpper())).ToList();
tmp = dialogResult;
}
if (message != "")
{
MessageBox.Show(message);
}
else
{
DialogCloser.SetDialogResult(wnd, true);
}
}
#endregion
}
EDIT : Updated the View with bindings set to TwoWay (solved)