ASP.NET Server control with an additional bindable field - c#

I have created a custom server control, deriving from System.Web.Contols.CheckBoxList to customize how a CheckBoxList is rendered. I also wanted to add another bindable field and get the value of the field within the CheckBoxList.RenderItem() method. The field I want to create, should contain a value specifying whether a CheckBoxListItem is checked. I've read some articles regarding custom DataFields, but it never gets explained in detail.
I've included a portion of my class to better explain what I can't seem to understand.
public class ListedCheckBoxList : CheckBoxList
{
protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
if (itemType != ListItemType.Item)
return;
var item = base.Items[repeatIndex];
string cbxHtml = string.Format("<input type=\"checkbox\" value=\"{0}\" name=\"{1}\" /> {2}",
item.Value,
string.Concat(this.ClientID, repeatIndex),
item.IsChecked, // <-- My custom bindable field
item.Text);
writer.Write(cbxHtml);
}
}
When using this control in the .aspx page, I'm attempting to bind it like this
<abc:ListedCheckBoxList ID="cbxList" runat="server"
DataValueField="UserId"
DataTextField="UserFullName"
DataIsCheckedField="UserIsActive" />

Here is a version I wrote a year or so ago. I wanted to be able to bind the checked status as well as a tooltip for the individual items. Hope it helps...
public class CheckBoxList_Extended : CheckBoxList
{
/// <summary>
/// Gets or sets the name of the data property to bind to the tooltip attribute of the individual CheckBox.
/// </summary>
[DefaultValue("")]
public string DataTooltipField
{
get
{
string value = base.ViewState["DataTooltipField"] as string;
if (value == null)
value = "";
return value;
}
set
{
if (value == null || value.Trim() == "")
{
base.ViewState.Remove("DataTooltipField");
}
else
{
base.ViewState["DataTooltipField"] = value.Trim();
}
}
}
/// <summary>
/// Gets or sets the name of the data property to bind to the Checked property of the individual CheckBox.
/// </summary>
[DefaultValue("")]
public string DataCheckedField
{
get
{
string value = base.ViewState["DataCheckedField"] as string;
if (value == null)
value = "";
return value;
}
set
{
if (value == null || value.Trim() == "")
{
base.ViewState.Remove("DataCheckedField");
}
else
{
base.ViewState["DataCheckedField"] = value.Trim();
}
}
}
protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
{
if (dataSource != null)
{
string dataSelectedField = this.DataCheckedField;
string dataTextField = this.DataTextField;
string dataTooltipField = this.DataTooltipField;
string dataValueField = this.DataValueField;
string dataTextFormatString = this.DataTextFormatString;
bool dataBindingFieldsSupplied = (dataTextField.Length != 0) || (dataValueField.Length != 0);
bool hasTextFormatString = dataTextFormatString.Length != 0;
bool hasTooltipField = dataTooltipField.Length != 0;
bool hasSelectedField = dataSelectedField.Length != 0;
if (!this.AppendDataBoundItems)
this.Items.Clear();
if (dataSource is ICollection)
this.Items.Capacity = (dataSource as ICollection).Count + this.Items.Count;
foreach (object dataItem in dataSource)
{
ListItem item = new ListItem();
if (dataBindingFieldsSupplied)
{
if (dataTextField.Length > 0)
{
item.Text = DataBinder.GetPropertyValue(dataItem, dataTextField, null);
}
if (dataValueField.Length > 0)
{
item.Value = DataBinder.GetPropertyValue(dataItem, dataValueField, null);
}
}
else
{
if (hasTextFormatString)
{
item.Text = string.Format(CultureInfo.CurrentCulture, dataTextFormatString, new object[] { dataItem });
}
else
{
item.Text = dataItem.ToString();
}
item.Value = dataItem.ToString();
}
if (hasSelectedField)
{
item.Selected = (bool)DataBinder.GetPropertyValue(dataItem, dataSelectedField);
}
if (hasTooltipField)
{
string tooltip = DataBinder.GetPropertyValue(dataItem, dataTooltipField, null);
if (tooltip != null && tooltip.Trim() != "")
{
item.Attributes["title"] = tooltip;
}
}
this.Items.Add(item);
}
}
base.PerformDataBinding(null);
}
}

Checkbox already has a property for that, "Checked"
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checked.aspx
You can add a custom fairly easily though, just add a new public property. You can then set it programatically or in the aspx code.
public class ListedCheckBoxList : CheckBoxList
{
public string CustomTag { get; set; }
//...snip
}
<myControls:myCheckBox runat='server' Checked='True' CustomTag="123test" />

Related

Add ToolTip to "Word" in a context menu

I want to add a tooltip to a menu item. On the menu there is the word "DELETE" and when the mouse hovers over the word I want a tooltip displayed. I though of using 'ToolTipService.SetToolTip();'.
This is where the items contained in the menu are set...
protected virtual void SetContextMenuItems()
{
// -- Add condition for ReadOnly + ReadOnly Attribute to AreaEntity
if (this.ViewMode == Common.Core.ViewModes.RealTime)
{
AreaEntity ae = viewModel.EntityViewContext as AreaEntity;
if (((UserContext.Instance.IsAdmin() && (ae.Scope.Value == "global" || ae.Scope.Value == string.Empty)) ||
ae.OwnerPosition.Value == CoreServices.Instance.CurrentPosition.Configuration.Name)
&& !((this.MapInstance.Parent as Grid).Parent is PIPMap))
{
menuItem = new ContextMenuItem();
//menuItem.DisplayText = "Delete"; // -- Could be dynamic based off type "Edit Polygon (Circle, etc.)"
menuItem.DisplayText = CoreServices.Instance.GetString("Delete");
cmd = new MR.CommandBridge.Common.Command.DelegateCommand(DeleteShape, CanDelete);
menuItem.Command = cmd;
this.ContextMenu.MenuItems.Add(menuItem);
}
}
}
Methods 'DeleteShape' and 'CanDelete':
public void DeleteShape(object param)
{
EntityStore.Instance.DeleteEntity(this.ViewModel.EntityViewContext);
}
public bool CanDelete(object param)
{
GetRulesForShape();
bool isInFilter = false;
EntityCollection<Entity> lists = EntitySync.Instance.Cache["entityCollection"];
foreach (Entity list in lists)
{
isInFilter = (list as ListEntity).FilterList.Filters.Count(a => (a.FilterType == FilterTypes.WithinZone && a.Value == this.viewModel.EntityViewContext.Uri) ||
(a.FilterType == FilterTypes.MultipleFilter && a.Filters.Count(b => b.FilterType == FilterTypes.WithinZone && b.Value == this.viewModel.EntityViewContext.Uri) > 0)) > 0;
if (isInFilter) break;
}
return !HasRules && !CoreServices.Instance.ZoneFilters.Contains(this.viewModel.Area.Uri) && gfEditor.dm != GeofenceEditor.DrawMode.DrawEdit && !isInFilter;
}
Ok I made some adjustments to your class.
Somehow I got the feeling your mixing up things like control and bindings.
We'll see. ;)
I've also made some comments, maybe you can shed some light over then.
public class ContextMenuItem : MenuItem
{
public ContextMenuItem()
:base()
{
}
//Replace by Header
//
//public string DisplayText { get; set; }
//Can this be replaced by build in CommandParameter
//
private Dictionary<string, object> _parameters = new Dictionary<string, object>();
private Func<ContextMenuItem, List<ContextMenuItem>> _getMenuItems = null;
//Already available
//public DelegateCommand Command { get; set; }
//What does this function do?
public Func<ContextMenuItem, List<ContextMenuItem>> GetMenuItems
{
get
{
return _getMenuItems;
}
set
{
_getMenuItems = value;
}
}
public Dictionary<string, object> Parameters
{
get
{
return _parameters;
}
}
//Can be replaced by base Items
//
//private List<ContextMenuItem> _menuItems = new List<ContextMenuItem>();
//public List<ContextMenuItem> ChildMenuItems
//{
// get
// {
// return _menuItems;
// }
//}
private bool _isChecked = false;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; }
}
// -- Command or implementer could provide a handler for all commands - might be simpler for now
// -- I think there could be a better way to route commands but I'll thin on it.
Could this simply be done in .css?
.yourclass:hover{
cursor:pointer;
}
or target it with jquery?
Have you tried this?
menuitem.ToolTip = "Delete";
Normally a contextmenu can exist of regular MenuItems. I used it often.
;)
Context menu items have the ToolTipText property:
menuItem.ToolTipText = "ToolTip Text Here";

Filter a property by the value of another property

I have two drop down lists. Niether of them have a relation ship with each other. But I need to filter one drop down list based on the chosen value of another drop down list.
I can filter it in code. When I debug I can see the filtered results on the property. However when I run the app, it does not work. Here is my code so far:
private BindingList<Commodity> _AllocationCommodities;
[Browsable(false)]
public BindingList<Commodity> AllocationCommodities
{
get
{
if (_AllocationCommodities == null)
{
_AllocationCommodities = new BindingList<Commodity>();
ChangeCommodities();
}
return _AllocationCommodities;
}
}
private SourceEntity _SourceEntity;
[ImmediatePostData]
[Association("SourceEntity-LimitAllocations")]
[RuleRequiredField("RuleRequiredField_LimitAllocation_SourceEntity", DefaultContexts.Save)]
public SourceEntity SourceEntity
{
get
{
return _SourceEntity;
}
set
{
//New Code
if (SetPropertyValue<SourceEntity>("SourceEntity", value))
{
if (IsSaving || IsLoading) return;
ChangeCommodities();
}
}
}
private Commodity _Commodity;// This is the drop down to be filtered
[ImmediatePostData]
[DataSourceProperty("AllocationCommodities")] //// This Attribute Should filter Commodities
[RuleRequiredField("RuleRequiredField_LimitAllocation_Commodity", DefaultContexts.Save)]
public Commodity Commodity
{
get
{
return _Commodity;
}
set
{
SetPropertyValue("Commodity", ref _Commodity, value);
if (Commodity.Oid != Guid.Empty)
AllocationVolumeUnits.Reload();
}
}
private void ChangeCommodities()
{
if (!this.IsLoading && _SourceEntity != null)
{
_AllocationCommodities.RaiseListChangedEvents = false;
_AllocationCommodities.Clear();
OperandValue[] _params;
System.Collections.Generic.List<CMSBOD.SourceCommodity> _sc = new System.Collections.Generic.List<SourceCommodity>();
BindingList<Commodity> _Commodities = new BindingList<Commodity>();
foreach (SourceCommodityEntity _tempSCE in _SourceEntity.SourceCommodityEntities)
{
if (_tempSCE.SourceCommodity != null)
_sc.Add(_tempSCE.SourceCommodity);
}
foreach (SourceCommodity _tempSC in _sc)
{
if (_tempSC.Commodity != null && !_Commodities.Contains<Commodity>(_tempSC.Commodity) && _tempSC.Commodity.IsActive)
_Commodities.Add(_tempSC.Commodity);
}
_AllocationCommodities.RaiseListChangedEvents = true;
_AllocationCommodities = _Commodities;///This is where I can see the filtered list when debugging.
}
}
You can find a DataSourceCriteria useful in this scenario, instead of DataSourceProperty.
Assuming you have collection properties that associates Commodity back to SourceCommodityEntity, you can use this criteria:
[DataSourceCriteria("IsActive And SourceCommodities[SourceCommodityEntities[SourceEntity = '#SourceEntity'] ]")]
Even if its designed to be a 1x1 assocation, you can find that associations can be useful for filtering purposes.

Filter a list based on many parameters and gather the result in another list

I have a list of objects. An object has the following properties:
public string mCardColor { get; set; }
public string mCardType { get; set; }
public string mCardRarity { get; set; }
In my view I have the possibility to filter directly the list obtained via a search engine using dropdownlists.
I then pass the values of the filters to the controller method and check if the request is an actual Ajax request like this:
public ActionResult DisplayCardsResults(string _rarity = "", string _type = "", string _color = "")
{
ViewBag._rarity = _rarity;
ViewBag._color = _color;
ViewBag._type = _type;
if (Request.IsAjaxRequest())
{
mListCardColors = null;
mListCardType = null;
mListCardRarity = null;
if (_rarity != "All")
{
mListCardRarity = mListCards.Where(_item => _item.mMasterCard.mCardRarity == _rarity).ToList();
}
if (_type != "All")
{
mListCardType =
mListCards.Where(_item => _item.mMasterCard.mCardType.ToLower().Contains(_type.ToLower())).ToList();
}
if (_color != "All")
{
mListCardColors = mListCards.Where(_item => _item.mMasterCard.mCardColor == _color).ToList();
}
if (mListCardType == null && mListCardColors == null && mListCardRarity == null)
{
return PartialView("_ResultsTable", mListCards.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
}
mListCardsToShow = new List<CardDisplay>();
if (mListCardType != null)
{
mListCardsToShow.AddRange(mListCardType);
}
if (mListCardRarity != null)
{
mListCardsToShow.AddRange(mListCardRarity);
}
if(mListCardColors != null)
{
mListCardsToShow.AddRange(mListCardColors);
}
return PartialView("_ResultsTable", mListCardsToShow.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
}
if (mListCardsToShow.Count > 0)
{
mListCardsToShow = SortListOrder(_sortOrder, mListCardsToShow);
return View(mListCardsToShow.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
}
if (mListCards.Count > 0)
{
mListCards = SortListOrder(_sortOrder, mListCards);
}
return View(mListCards.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
}
You have 2 list: the mListCards is the list of cards obtained from the search engine. This does not need to change. The mListCardsToShow is used only if the request is an Ajax request.
I want to retain only the values wanted based on the filters passed to the controller method. The principle is the following: if the three dropdownlist are on all, show all cards. But if there's a value in any or all the dropdownlist, the actual list needs to be filtered.
Is there an efficient way to filter the list based on the three param using Linq other than to write 9 differents scenarios?
If you don't have a strong reason not to, it probably makes sense to filtering on the three fields simultaneously:
var filteredCards =
from card in mListCards
where _color == "ALL" || card.mCardColor == _color
where _type == "ALL" || card.mCardType == _type
where _rarity == "ALL" || card.mCardRarity == _rarity
select card;

RaisePropertyChanged not updating UI

I'm having trouble getting my UI to update Two Listboxes' to update properly when my ViewModel changes.
First, the basic logic behind the page:
Movie is an object with a title, and a variety of MovieDetails. Some MovieDetail are different than others, as they are detailed which is a glorified way of saying they're more Important.
I use two ListBoxes to separate these MovieDetails into stacked ListBoxes, one for 'Detailed' and one for 'NotDetailed'
If a movie has no 'Detailed' attributes, the corresponding list is Hidden via a BooleanToVisibilityConverter (and vice-versa)
When I navigate to the page, I set the Movie the page corresponds to, and it should RaisePropertyChanged to alert the AllMoviesDetail ObservableCollection that it should re-get Movies.MovieDetailFetchedList.
From there, AllMoviesDetail would alert the two ObservableCollections (Detailed, NotDetailed) they should be re-get.
In fact, RaisePropertyChanged on NotDetailedMovieDetails or DetailedMovieDetails does not seem to do anything either. (And the corresponding HasNotDetailedMovieDetails, Has...)
What does work, however, is if I add more items into the list, the CollectionChanged event seems to fire and reactivate the list. I have also been able to do this by instantiating the ObservableCollections in code first var temp = DetailedMoviesDetail;
public class MoviesDetailViewModel : ViewModelBase
{
#region Property Names
public const string MoviePropertyString = "Movie";
public const string AllMoviesDetailPropertyString = "AllMoviesDetail";
public const string DetailedMoviesDetailPropertyString = "DetailedMoviesDetail";
public const string NotDetailedMoviesDetailPropertyString = "NotDetailedMoviesDetail";
public const string HasNotDetailedMoviesDetailPropertyString = "HasNotDetailedMoviesDetail";
public const string HasDetailedMoviesDetailPropertyString = "HasDetailedMoviesDetail";
public const string NotDetailedHeaderPropertyString = "NotDetailedHeader";
#endregion
public MoviesDetailViewModel()
{
if (IsInDesignMode)
{
Movie = DesignDataStore.MovieList[0];
Movie.Category = Category.DDA;
}
}
private Movie _Movie;
/// <summary>
/// The Movie for which to browse MoviesDetail. It is expected when setting this property, that MoviesDetail for it have been downloaded previously.
/// </summary>
/// <remarks>The 'Master' property for this ViewModel. All properties are Dependent on this and the underlying property MoviesDetailList</remarks>
/// <seealso cref="MovieDetailFetchedList"/>
public Movie Movie
{
get { return _Movie; }
set
{
if (_Movie != value)
{
if (_Movie != null)
_Movie.MovieDetailFetchedList.CollectionChanged -= MoviesDetailListChanged;
_Movie = value;
RaisePropertyChanged(MoviePropertyString);
RaisePropertyChanged(StatementPeriodAvailablePropertyString);
RaisePropertyChanged(NotDetailedMoviesDetailPropertyString);
Movie.MovieDetailFetchedList.CollectionChanged += MoviesDetailListChanged;
RaisePropertyChanged(AllMoviesDetailPropertyString);
RaisePropertyChanged(DetailedMoviesDetailPropertyString);
RaisePropertyChanged(NotDetailedHeaderPropertyString);
}
}
}
private void MoviesDetailListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (var item in e.NewItems)
{
if (((MovieDetail) item).IsDetailed())
DetailedMoviesDetail.Add(item as MovieDetail);
else
NotDetailedMoviesDetail.Add(item as MovieDetail);
}
}
else
{
RaisePropertyChanged(AllMoviesDetailPropertyString);
RaisePropertyChanged(DetailedMoviesDetailPropertyString);
RaisePropertyChanged(NotDetailedMoviesDetailPropertyString);
}
}
#endregion
private MovieDetailFetchedList _allMoviesDetail;
public MovieDetailFetchedList AllMoviesDetail
{
get
{
if (Movie == null)
return new MovieDetailFetchedList();
return _allMoviesDetail ?? (AllMoviesDetail = Movie.MovieDetailFetchedList);
}
set
{
if (_allMoviesDetail != value)
{
if (_allMoviesDetail != null)
_allMoviesDetail.CollectionChanged -= MoviesDetailListChanged;
_allMoviesDetail = value;
_allMoviesDetail.CollectionChanged += MoviesDetailListChanged;
RaisePropertyChanged(AllMoviesDetailPropertyString);
//force update
DetailedMoviesDetail = NotDetailedMoviesDetail = null;
RaisePropertyChanged(DetailedMoviesDetailPropertyString);
RaisePropertyChanged(HasDetailedMoviesDetailPropertyString);
RaisePropertyChanged(NotDetailedMoviesDetailPropertyString);
RaisePropertyChanged(HasNotDetailedMoviesDetailPropertyString);
}
}
}
public bool HasNotDetailedMoviesDetail { get { return NotDetailedMoviesDetail != null && NotDetailedMoviesDetail.Count > 0; } }
private ObservableCollection<MovieDetail> _notDetailedMoviesDetail;
public ObservableCollection<MovieDetail> NotDetailedMoviesDetail
{
get
{
if (Movie == null) return new ObservableCollection<MovieDetail>();
return AllMoviesDetail;
return _notDetailedMoviesDetail ?? //make sure RaisePropertyChanged happens by using property setter
(NotDetailedMoviesDetail = AllMoviesDetail.Where(mem => !mem.IsDetailed()).ToObservableCollection());
}
set
{
_notDetailedMoviesDetail = value;
RaisePropertyChanged(NotDetailedMoviesDetailPropertyString);
RaisePropertyChanged(HasNotDetailedMoviesDetailPropertyString);
}
}
public bool HasDetailedMoviesDetail
{ get { return DetailedMoviesDetail != null && DetailedMoviesDetail.Count > 0; } }
private ObservableCollection<MovieDetail> _DetailedMoviesDetail;
public ObservableCollection<MovieDetail> DetailedMoviesDetail
{
get
{
if (Movie == null) return new ObservableCollection<MovieDetail>();
return AllMoviesDetail;
return _DetailedMoviesDetail ?? //make sure RaisePropertyChanged happens by using property setter
(DetailedMoviesDetail = AllMoviesDetail.Where(mem => mem.IsDetailed()).ToObservableCollection());
}
set
{
_DetailedMoviesDetail = value;
RaisePropertyChanged(DetailedMoviesDetailPropertyString);
RaisePropertyChanged(HasDetailedMoviesDetailPropertyString);
}
}
private string _DetailedHeader;
public string DetailedHeader
{
get { return _DetailedHeader ?? (_DetailedHeader = AppResources.in_available); }
set { _DetailedHeader = value; }
}
public string NotDetailedHeader
{
get { return (Movie != null && Movie.Category == Category.DRAMA) ? AppResources.Movie_MoviesDetail : AppResources.not_in_available; }
}
}
All of your property getters (except AllMoviesDetail) have two return statements. Since only the first will be executed, the values are not being assigned and the PropertyChanged events are not being twiggered.

Enable Disable save button during Validation using IDataErrorInfo

How to disable/enable a button while doing validation using IDataErrorInfo?
I am using MVVM using GalaSoft light Framework. In my Model class I have implemented IDataErrorInfo to display the error messages.
public string this[string columnName]
{
get
{
Result = null;
if (columnName == "FirstName")
{
if (String.IsNullOrEmpty(FirstName))
{
Result = "Please enter first name";
}
}
else if (columnName == "LastName")
{
if (String.IsNullOrEmpty(LastName))
{
Result = "Please enter last name";
}
}
else if (columnName == "Address")
{
if (String.IsNullOrEmpty(Address))
{
Result = "Please enter Address";
}
}
else if (columnName == "City")
{
if (String.IsNullOrEmpty(City))
{
Result = "Please enter city";
}
}
else if (columnName == "State")
{
if (State == "Select")
{
Result = "Please select state";
}
}
else if (columnName == "Zip")
{
if (String.IsNullOrEmpty(Zip))
{
Result = "Please enter zip";
}
else if (Zip.Length < 6)
{
Result = "Zip's length has to be at least 6 digits!";
}
else
{
bool zipNumber = Regex.IsMatch(Zip, #"^[0-9]*$");
if (zipNumber == false)
{
Result = "Please enter only digits in zip";
}
}
}
else if (columnName == "IsValid")
{
Result = true.ToString();
}
return Result;
}
}
Screenshot: http://i.stack.imgur.com/kwEI8.jpg
How to disable/enable save button. Kindly suggest?
Thanks
The Josh Smith Way of doing this is to create the following methods in the Model:
static readonly string[] ValidatedProperties =
{
"Foo",
"Bar"
};
/// <summary>
/// Returns true if this object has no validation errors.
/// </summary>
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
{
if (GetValidationError(property) != null) // there is an error
return false;
}
return true;
}
}
private string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "Foo":
error = this.ValidateFoo();
break;
case "Bar":
error = this.ValidateBar();
break;
default:
error = null;
throw new Exception("Unexpected property being validated on Service");
}
return error;
}
The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:
/// <summary>
/// Checks if all parameters on the Model are valid and ready to be saved
/// </summary>
protected bool CanSave
{
get
{
return modelOfThisVM.IsValid;
}
}
Finally, if you are using RelayCommand, you can set the predicate of the command to the CanSave property, and the View will automatically enable or disable the button. In the ViewModel:
/// <summary>
/// Saves changes Command
/// </summary>
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
_saveCommand = new RelayCommand(param => this.SaveChanges(), param => this.CanSave);
return _saveCommand;
}
}
And in the View:
<Button Content="Save" Command="{Binding Path=SaveCommand}"/>
And that's it!
PS: If you haven't read Josh Smith's article yet, it will change your life.
you can add add a boolean property CanSave and set it at the end of your valiation method. Bind the IsEnabled from your button to IsValid.
Somthing like this:
public bool CanSave
{
get{ return canSave; }
set{ canSave = value; RaisePropertyChanged( "CanSave" ); }
}
private bool canSave;
public string this[string columnName]
{
//....
CanSave = Result == String.Empty;
}
//xaml
<Button IsEnabled={Binding Path=CanSave}>Save</Button>
Here is my way of doing it using a combination of IDataErrorInfo interface, ValidationErrors Dictionary, and MVVM-Light messaging system. Straight forward and works like charm:
Model Class
public Dictionary<string, string> ValidationErrors = new Dictionary<string, string>();
public string this[string columnName]
{
get
{
// Remove Property error from ValidationErrors prior to any validation
ValidationErrors.Remove(propertyName);
//----------------------------------------
string Result = null;
if (columnName == "FirstName")
{
if (String.IsNullOrEmpty(FirstName))
{
// Add Property error to ValidationErrors Dic
ValidationErrors[propertyName] = Result = "Please enter first name";
//----------------------------------------
}
}
else if (columnName == "LastName")
{
if (String.IsNullOrEmpty(LastName))
{
// Add Property error to ValidationErrors Dic
ValidationErrors[propertyName] = Result = "Please enter last name";
//----------------------------------------
}
}
// Send MVVM-Light message and receive it in the Code Behind or VM
Messenger.Default.Send<PersonInfoMsg>(new PersonInfoMsg());
//----------------------------------------
return Result;
}
}
View Code Behind
public partial class PersonInfoView : UserControl
{
public PersonInfoView()
{
InitializeComponent();
Messenger.Default.Register<PersonInfoMsg>(this, OnErrorMsg);
}
private void OnErrorMsg(PersonInfoMsg)
{
// In case of DataGrid validation
foreach (PersonInfoModel p in GridName.ItemsSource)
{
if (p.ValidationErrors.Count == 0)
SaveBtn.IsEnabled = true;
else
SaveBtn.IsEnabled = false;
}
}
}

Categories

Resources