Click here to Skip to main content
15,885,546 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: How to make listbox list items in horizontal Pin
SledgeHammer012-Nov-11 6:32
SledgeHammer012-Nov-11 6:32 
QuestionSivlerlight: the best alternative to a simple WPF-like DataTrigger? Pin
Member 103390731-Oct-11 4:51
Member 103390731-Oct-11 4:51 
AnswerRe: Sivlerlight: the best alternative to a simple WPF-like DataTrigger? Pin
Pete O'Hanlon31-Oct-11 7:59
mvePete O'Hanlon31-Oct-11 7:59 
GeneralRe: Sivlerlight: the best alternative to a simple WPF-like DataTrigger? Pin
Member 103390731-Oct-11 23:16
Member 103390731-Oct-11 23:16 
GeneralRe: Sivlerlight: the best alternative to a simple WPF-like DataTrigger? Pin
Pete O'Hanlon1-Nov-11 0:29
mvePete O'Hanlon1-Nov-11 0:29 
GeneralRe: Sivlerlight: the best alternative to a simple WPF-like DataTrigger? Pin
Member 10339071-Nov-11 2:11
Member 10339071-Nov-11 2:11 
GeneralRe: Sivlerlight: the best alternative to a simple WPF-like DataTrigger? Pin
Pete O'Hanlon1-Nov-11 2:43
mvePete O'Hanlon1-Nov-11 2:43 
QuestionValidation of a combobox in Silverlight 4 Pin
Phillip Donegan31-Oct-11 2:46
Phillip Donegan31-Oct-11 2:46 
I'm trying to understand how validation works for a combo box when its ItemsSource is bound to a ObserableCollection of complex types. I am using RIA as the serivce to connect the client tier to the middle tier. Also not sure if this makes a difference the combobox control is inside a dataform. I have done alot of reading on this and found this article to be the most useful: http://www.run80.net/?p=93

So firstly my entity: I have a field decorated like so:


C#
[Required]
    public virtual long FrequencyId { get; set; }

    [Include]
    [Association("TreatmentFrequencyToTreatmentRecordAssociation", "FrequencyId", "Id", IsForeignKey = true)]
    public virtual TreatmentFrequency Frequency
    {
        get
        {
            return this.frequency;
        }

        set
        {
            this.frequency = value;

            if (value != null)
            {
                this.FrequencyId = value.Id;
            }
        }
    }


Now I belive that I cannot set the [Required] annotation on an association but instead on the foreign key id (what the above article says).

The actual Treatment Frequency class looks like this:

C#
public class TreatmentFrequency
    {
        [Key]
        public virtual long Id { get; set; }

        [Required]
        [StringLength(10)]
        public virtual string Code { get; set; }

        [Required]
        [StringLength(40)]
        public virtual string Name { get; set; }

        public override bool Equals(object obj)
        {
            obj = obj as TreatmentFrequency;
            if (obj == null)
            {
                return false;
            }

            return this.Id == ((TreatmentFrequency)obj).Id;
        }

        public override int GetHashCode()
        {
            return this.Name.GetHashCode();
        }
    }


I have overriden the Equals and GetHashCode method becuase in another article it said that when in a collection you need to override the equals to match on the key otherwise when you use SelectedItem although all the values would be the same between the item in the collection and the selecteditem they would be two different instances and thus not match with the default implementation of Equals.

Now my xaml looks like this:

XML
<df:DataField Label="Frequency">
    <ComboBox SelectedItem="{Binding Path=CurrentItem.Frequency, Mode=TwoWay}" ItemsSource="{Binding Path=Frequencies}" DisplayMemberPath="Name" SelectedValue="{Binding Path=CurrentItem.FrequencyId, Mode=TwoWay}" SelectedValuePath="Id"/>
</df:DataField>


To be honest the above doesn't make much sense to me, I could remove SelectedValue and SelectedValuePath and the form would still work as expected (without validation) I thought that Selected Value would point to the complex type E.g. CurrentItem.Frequency and then the SelectedValuePath would be the underlying "Name" property. However I also understand what the author is trying to do in that the [Required] tag isn't on the association but the foreign key id E.g. CurrentItem.FrequencyId, so it must have to go somewhere.

Now a final compelexity is that this form is part of a wizard so I am not able to validate the entire object, instead I manually have to validate certain field which are only being populated in this particular wizard step. To do this I created the method:

C#
public void ValidateProperty(object value, string propertyName)
    {
        var results = new List();

        Validator.TryValidateProperty(value, new ValidationContext(this.TreatmentRecord, null, null) { MemberName = propertyName }, results);

        foreach (var error in results)
        {
            this.TreatmentRecord.ValidationErrors.Add(error);
        }
    }


In my view model I have a method IsValid which is called before the wizard is allowed to navigate to the next step and then I call the above method like so:

C#
public bool IsValid
    {
        get
        {
            this.treatmentRecordWizardContext.ValidateProperty(this.treatmentRecordWizardContext.TreatmentRecord.Frequency, "Frequency");
            this.treatmentRecordWizardContext.ValidateProperty(this.treatmentRecordWizardContext.TreatmentRecord.FrequencyId, "FrequencyId");

            this.OnPropertyChanged(() => this.CurrentItem);

            if (this.treatmentRecordWizardContext.TreatmentRecord.ValidationErrors.Count == 0)
            {
                return true;
            }

            return false;
        }
    }


With all of the above code the validation is completly ignored when the combobox is left empty. I have not templated the combobox itself so I am really at a loss as to why its not working and really which part of the solution is at fault, is it the bindings or is it the entitys in the RIA not defined correctly!

Hope someone can help I've spent far too long trying to get this to work, I assume this must be done reqularly by other developers so I'm hoping its a simple fix.
AnswerRe: Validation of a combobox in Silverlight 4 Pin
Phillip Donegan1-Nov-11 22:54
Phillip Donegan1-Nov-11 22:54 
QuestionBlinking Pushpin in Bing Map Silverlight control Pin
Majid Shahabfar30-Oct-11 2:26
Majid Shahabfar30-Oct-11 2:26 
QuestionHow to Bind Canvas.Left Property Pin
Turhan Coskun28-Oct-11 3:51
professionalTurhan Coskun28-Oct-11 3:51 
GeneralRe: How to Bind Canvas.Left Property Pin
Turhan Coskun28-Oct-11 4:07
professionalTurhan Coskun28-Oct-11 4:07 
AnswerRe: How to Bind Canvas.Left Property Pin
Turhan Coskun28-Oct-11 10:09
professionalTurhan Coskun28-Oct-11 10:09 
QuestionProblem filtering Data in DomainService query Pin
jadughar28-Oct-11 0:06
jadughar28-Oct-11 0:06 
AnswerRe: Problem filtering Data in DomainService query Pin
jadughar28-Oct-11 3:13
jadughar28-Oct-11 3:13 
QuestionBinding RadTreeView SelectedItem in Code Behind Pin
Kevin Marois27-Oct-11 8:20
professionalKevin Marois27-Oct-11 8:20 
AnswerRe: Binding RadTreeView SelectedItem in Code Behind Pin
SledgeHammer0127-Oct-11 8:40
SledgeHammer0127-Oct-11 8:40 
GeneralRe: Binding RadTreeView SelectedItem in Code Behind Pin
Kevin Marois27-Oct-11 8:43
professionalKevin Marois27-Oct-11 8:43 
AnswerRe: Binding RadTreeView SelectedItem in Code Behind Pin
Mark Salsbery27-Oct-11 8:46
Mark Salsbery27-Oct-11 8:46 
QuestionSetting TextBlock width from code behind Pin
indian14326-Oct-11 7:20
indian14326-Oct-11 7:20 
AnswerRe: Setting TextBlock width from code behind Pin
Mark Salsbery26-Oct-11 7:23
Mark Salsbery26-Oct-11 7:23 
GeneralRe: Setting TextBlock width from code behind Pin
indian14326-Oct-11 7:53
indian14326-Oct-11 7:53 
GeneralRe: Setting TextBlock width from code behind Pin
SledgeHammer0126-Oct-11 8:48
SledgeHammer0126-Oct-11 8:48 
GeneralRe: Setting TextBlock width from code behind Pin
Kim Breugelmans26-Oct-11 10:21
Kim Breugelmans26-Oct-11 10:21 
AnswerRe: Setting TextBlock width from code behind Pin
AspDotNetDev26-Oct-11 10:17
protectorAspDotNetDev26-Oct-11 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.