Click here to Skip to main content
15,943,161 members
Home / Discussions / WPF
   

WPF

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 3:09
cofounderChris Maunder16-Jul-09 3:09 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 3:05
cofounderChris Maunder16-Jul-09 3:05 
QuestionHow to create and save High Score in my Tetris game in wpf c#? Pin
Member 162860911-Jul-24 1:03
Member 162860911-Jul-24 1:03 
AnswerRe: How to create and save High Score in my Tetris game in wpf c#? Pin
Richard MacCutchan1-Jul-24 1:20
mveRichard MacCutchan1-Jul-24 1:20 
Questionselect text box lines and remove Pin
geomeo12322-Jun-24 16:30
geomeo12322-Jun-24 16:30 
AnswerRe: select text box lines and remove Pin
Richard Deeming23-Jun-24 21:47
mveRichard Deeming23-Jun-24 21:47 
GeneralRe: select text box lines and remove Pin
geomeo12324-Jun-24 7:52
geomeo12324-Jun-24 7:52 
QuestionCustom Control Based On Combobox - Replace Part Of The Combobox Pin
Kevin Marois14-Jun-24 18:27
professionalKevin Marois14-Jun-24 18:27 
AnswerRe: Custom Control Based On Combobox - Replace Part Of The Combobox Pin
Dave Kreskowiak15-Jun-24 5:48
mveDave Kreskowiak15-Jun-24 5:48 
GeneralRe: Custom Control Based On Combobox - Replace Part Of The Combobox Pin
Kevin Marois15-Jun-24 10:13
professionalKevin Marois15-Jun-24 10:13 
QuestionInvoke WPF Pin
geomeo1231-Jun-24 15:31
geomeo1231-Jun-24 15:31 
AnswerRe: Invoke WPF Pin
Richard Deeming3-Jun-24 21:38
mveRichard Deeming3-Jun-24 21:38 
GeneralRe: Invoke WPF Pin
geomeo1234-Jun-24 17:54
geomeo1234-Jun-24 17:54 
QuestionICommand Wpf form load properties? Pin
geomeo12330-May-24 14:11
geomeo12330-May-24 14:11 
AnswerRe: ICommand Wpf form load properties? Pin
Richard Deeming30-May-24 21:26
mveRichard Deeming30-May-24 21:26 
GeneralRe: ICommand Wpf form load properties? Pin
geomeo12331-May-24 16:10
geomeo12331-May-24 16:10 
GeneralRe: ICommand Wpf form load properties? Pin
Richard Deeming3-Jun-24 21:35
mveRichard Deeming3-Jun-24 21:35 
The typical solution would be to use an "attached behaviour" to bind the Loaded event to your command.
Introduction to Attached Behaviors in WPF[^]

For example:
C#
public static class LoadedBehavior
{
    public static readonly DependencyProperty LoadedCommandProperty = DependencyProperty.RegisterAttached(
        "LoadedCommand",
        typeof(ICommand),
        typeof(LoadedBehavior),
        new PropertyMetadata(null, OnLoadedCommandChanged));
    
    public static ICommand GetLoadedCommand(FrameworkElement obj)
    {
        return (ICommand)obj.GetValue(LoadedCommandProperty);
    }
    
    public static void SetLoadedCommand(FrameworkElement obj, ICommand value)
    {
        obj.SetValue(LoadedCommandProperty, value);
    }
    
    private static void OnLoadedCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = obj as FrameworkElement;
        if (element == null) return;
        
        if (e.OldValue == null)
        {
            element.Loaded += OnLoaded;
        }
        else if (e.NewValue == null)
        {
            element.Loaded -= OnLoaded;
        }
    }
    
    public static readonly DependencyProperty LoadedCommandParameterProperty = DependencyProperty.RegisterAttached(
        "LoadedCommandParameter",
        typeof(object),
        typeof(LoadedBehavior),
        new PropertyMetadata(null));
    
    public static object GetLoadedCommandParameter(FrameworkElement obj)
    {
        return obj.GetValue(LoadedCommandParameterProperty);
    }
    
    public static void SetLoadedCommandParameter(FrameworkElement obj, object value)
    {
        obj.SetValue(LoadedCommandParameterProperty, value);
    }
    
    private static void OnLoaded(object sender, RoutedEventArgs e)
    {
        FrameworkElement element = sender as FrameworkElement;
        if (element == null) return;
        
        ICommand command = GetLoadedCommand(element);
        if (command == null) return;
        
        object parameter = GetLoadedCommandParameter(element);
        command.Execute(parameter);
    }
}




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

GeneralRe: ICommand Wpf form load properties? Pin
Richard Deeming3-Jun-24 22:57
mveRichard Deeming3-Jun-24 22:57 
GeneralRe: ICommand Wpf form load properties? Pin
geomeo1234-Jun-24 17:46
geomeo1234-Jun-24 17:46 
QuestionWrapPanel / ItemsControl Problem Pin
Kevin Marois29-May-24 16:15
professionalKevin Marois29-May-24 16:15 
AnswerRe: WrapPanel / ItemsControl Problem Pin
Richard Deeming29-May-24 21:41
mveRichard Deeming29-May-24 21:41 
GeneralRe: WrapPanel / ItemsControl Problem Pin
Kevin Marois30-May-24 7:07
professionalKevin Marois30-May-24 7:07 
GeneralRe: WrapPanel / ItemsControl Problem Pin
Richard Deeming30-May-24 21:22
mveRichard Deeming30-May-24 21:22 
QuestionInitialize Webview2 in WPF Application work for windows user authentication Pin
Aditi Arora 202116-May-24 8:05
Aditi Arora 202116-May-24 8:05 
AnswerRe: Initialize Webview2 in WPF Application work for windows user authentication Pin
Dave Kreskowiak16-May-24 9:33
mveDave Kreskowiak16-May-24 9:33 

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.