Click here to Skip to main content
15,886,873 members
Home / Discussions / WPF
   

WPF

 
QuestionHow To Call A Method In A User Control Pin
Kevin Marois1-Aug-14 8:01
professionalKevin Marois1-Aug-14 8:01 
AnswerRe: How To Call A Method In A User Control Pin
Mycroft Holmes1-Aug-14 13:38
professionalMycroft Holmes1-Aug-14 13:38 
GeneralRe: How To Call A Method In A User Control Pin
Kevin Marois4-Aug-14 6:13
professionalKevin Marois4-Aug-14 6:13 
QuestionRe: How To Call A Method In A User Control Pin
Richard Deeming4-Aug-14 6:53
mveRichard Deeming4-Aug-14 6:53 
AnswerRe: How To Call A Method In A User Control Pin
Kevin Marois4-Aug-14 6:57
professionalKevin Marois4-Aug-14 6:57 
GeneralRe: How To Call A Method In A User Control Pin
Richard Deeming4-Aug-14 7:02
mveRichard Deeming4-Aug-14 7:02 
GeneralRe: How To Call A Method In A User Control Pin
Kevin Marois4-Aug-14 7:03
professionalKevin Marois4-Aug-14 7:03 
GeneralRe: How To Call A Method In A User Control Pin
Richard Deeming4-Aug-14 7:16
mveRichard Deeming4-Aug-14 7:16 
Well, if you're not going to create a VM for the UC, and you need to return a value from the method, then you're stuck with hooking the method up to the VM somehow.

If you don't want to use the code-behind, then you might be able to get away with binding a delegate. (NB: I haven't tried this.)

UC's code behind:
C#
public ValidationResult Validate() { ... }

public Func<ValidationResult> ValidationMethod
{
    get { return Validate; }
}


VM:
C#
public Func<ValidationResult> ValidationMethod { get; set; }

private ValidationResult CallValidation()
{
    Func<ValidationResult> fn = ValidationMethod;
    if (fn == null) return default(ValidationResult);
    return fn();
}


View:
XML
<ns:UserControl
    ValidationMethod="{Bind ValidationMethod, Mode=OneWayToSource}"
/>


If the binding doesn't work, then you'll have to hook the method up from the code-behind of the view:
C#
public MyView()
{
    InitializeComponent();
    DataContextChanged += OnDataContextChanged;
}

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var context = e.NewValue as MyViewViewModel;
    if (context != null) context.ValidationMethod = theUserControl.ValidationMethod;
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: How To Call A Method In A User Control Pin
SledgeHammer014-Aug-14 10:07
SledgeHammer014-Aug-14 10:07 
GeneralRe: How To Call A Method In A User Control Pin
Kevin Marois4-Aug-14 10:10
professionalKevin Marois4-Aug-14 10:10 
GeneralRe: How To Call A Method In A User Control Pin
SledgeHammer014-Aug-14 10:46
SledgeHammer014-Aug-14 10:46 
GeneralRe: How To Call A Method In A User Control Pin
Kevin Marois4-Aug-14 10:48
professionalKevin Marois4-Aug-14 10:48 
AnswerRe: How To Call A Method In A User Control Pin
_Maxxx_4-Aug-14 18:38
professional_Maxxx_4-Aug-14 18:38 
GeneralRe: How To Call A Method In A User Control Pin
SledgeHammer016-Aug-14 9:27
SledgeHammer016-Aug-14 9:27 
QuestionHow to send a fax using wpf? Pin
Kesavanksk24-Jul-14 23:41
Kesavanksk24-Jul-14 23:41 
AnswerRe: How to send a fax using wpf? Pin
Pete O'Hanlon25-Jul-14 0:54
mvePete O'Hanlon25-Jul-14 0:54 
GeneralRe: How to send a fax using wpf? Pin
Mycroft Holmes25-Jul-14 14:31
professionalMycroft Holmes25-Jul-14 14:31 
AnswerRe: How to send a fax using wpf? Pin
Afzaal Ahmad Zeeshan15-Aug-14 6:46
professionalAfzaal Ahmad Zeeshan15-Aug-14 6:46 
QuestionUnable to access DatePicker's SelectedDate property Pin
Suj_7822-Jul-14 3:26
Suj_7822-Jul-14 3:26 
AnswerRe: Unable to access DatePicker's SelectedDate property Pin
SledgeHammer0123-Jul-14 5:14
SledgeHammer0123-Jul-14 5:14 
QuestionProblème with TreeView Pin
Mouaici Mohamed22-Jul-14 1:38
Mouaici Mohamed22-Jul-14 1:38 
AnswerRe: Problème with TreeView Pin
Mycroft Holmes23-Jul-14 1:08
professionalMycroft Holmes23-Jul-14 1:08 
GeneralRe: Problème with TreeView Pin
Mouaici Mohamed23-Jul-14 1:14
Mouaici Mohamed23-Jul-14 1:14 
GeneralRe: Problème with TreeView Pin
Mycroft Holmes23-Jul-14 3:00
professionalMycroft Holmes23-Jul-14 3:00 
QuestionPass a window to a dialog as owner Pin
Mycroft Holmes21-Jul-14 22:54
professionalMycroft Holmes21-Jul-14 22:54 

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.