AttachedProperty vs behavior to get SelectedItems in the ViewModel












0















I have to options to get the SelectedItems in the ViewModel.



A attached property like this:



public class ListBoxSelectedItemsAttachedProperty
{
#region SelectedItems
///
/// SelectedItems Attached Dependency Property
///
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
typeof(ListBoxSelectedItemsAttachedProperty),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnSelectedItemsChanged)));

public static IList GetSelectedItems(DependencyObject d)
{
return (IList)d.GetValue(SelectedItemsProperty);
}

public static void SetSelectedItems(DependencyObject d, IList value)
{
d.SetValue(SelectedItemsProperty, value);
}

private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListBox miLb = (ListBox)d;
miLb.SelectionChanged += listBox_SelectionChanged;
}

private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox miLg = (ListBox)sender;
//Get list box's selected items.
IEnumerable miDgSelectedItems = miLg.SelectedItems;
//Get list from model
IList ModelSelectedItems = GetSelectedItems(miLg);

//Update the model
ModelSelectedItems.Clear();

if (miLg.SelectedItems != null)
{
foreach (var item in miLg.SelectedItems)
ModelSelectedItems.Add(item);
}
SetSelectedItems(miLg, ModelSelectedItems);
}
#endregion
}


And in the axml it is used in this way, for example in a Listbox:



Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding MyPropertyInViewModel}"


Attached Behavoir:



public class SelectedItemsBehavior : Behavior<MultiSelector>
{
protected override void OnAttached()
{
AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= AssociatedObjectSelectionChanged;
}

void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<object> selectedItemList = AssociatedObject.SelectedItems.Cast<object>().ToList();
ObservableCollection<object> selectedItems = new ObservableCollection<object>(selectedItemList);
SelectedItems = selectedItems;
}
public ObservableCollection<object> SelectedItems
{
get { return (ObservableCollection<object>)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems"
, typeof(ObservableCollection<object>)
, typeof(SelectedItemsBehavior)
,
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}


And in the axml is used in this way, for example in a DataGrid:



<i:Interaction.Behaviors>
<Behaviors:SelectedItemsBehavior SelectedItems="{Binding MyPropertyInViewModel}" />
</i:Interaction.Behaviors>


But Really I don't know the differences between an attached property and an attached behavior, and what is the best option to get the SelectedItems in the ViewModel.



Thanks.










share|improve this question























  • Why not just bind the SelectedItems property of the control to a view model property? That's what MVVM is all about, and is waaaay simpler than either of the options you have shown (which are both overkill for the requirement)

    – Avrohom Yisroel
    Nov 17 '18 at 23:01
















0















I have to options to get the SelectedItems in the ViewModel.



A attached property like this:



public class ListBoxSelectedItemsAttachedProperty
{
#region SelectedItems
///
/// SelectedItems Attached Dependency Property
///
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
typeof(ListBoxSelectedItemsAttachedProperty),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnSelectedItemsChanged)));

public static IList GetSelectedItems(DependencyObject d)
{
return (IList)d.GetValue(SelectedItemsProperty);
}

public static void SetSelectedItems(DependencyObject d, IList value)
{
d.SetValue(SelectedItemsProperty, value);
}

private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListBox miLb = (ListBox)d;
miLb.SelectionChanged += listBox_SelectionChanged;
}

private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox miLg = (ListBox)sender;
//Get list box's selected items.
IEnumerable miDgSelectedItems = miLg.SelectedItems;
//Get list from model
IList ModelSelectedItems = GetSelectedItems(miLg);

//Update the model
ModelSelectedItems.Clear();

if (miLg.SelectedItems != null)
{
foreach (var item in miLg.SelectedItems)
ModelSelectedItems.Add(item);
}
SetSelectedItems(miLg, ModelSelectedItems);
}
#endregion
}


And in the axml it is used in this way, for example in a Listbox:



Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding MyPropertyInViewModel}"


Attached Behavoir:



public class SelectedItemsBehavior : Behavior<MultiSelector>
{
protected override void OnAttached()
{
AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= AssociatedObjectSelectionChanged;
}

void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<object> selectedItemList = AssociatedObject.SelectedItems.Cast<object>().ToList();
ObservableCollection<object> selectedItems = new ObservableCollection<object>(selectedItemList);
SelectedItems = selectedItems;
}
public ObservableCollection<object> SelectedItems
{
get { return (ObservableCollection<object>)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems"
, typeof(ObservableCollection<object>)
, typeof(SelectedItemsBehavior)
,
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}


And in the axml is used in this way, for example in a DataGrid:



<i:Interaction.Behaviors>
<Behaviors:SelectedItemsBehavior SelectedItems="{Binding MyPropertyInViewModel}" />
</i:Interaction.Behaviors>


But Really I don't know the differences between an attached property and an attached behavior, and what is the best option to get the SelectedItems in the ViewModel.



Thanks.










share|improve this question























  • Why not just bind the SelectedItems property of the control to a view model property? That's what MVVM is all about, and is waaaay simpler than either of the options you have shown (which are both overkill for the requirement)

    – Avrohom Yisroel
    Nov 17 '18 at 23:01














0












0








0








I have to options to get the SelectedItems in the ViewModel.



A attached property like this:



public class ListBoxSelectedItemsAttachedProperty
{
#region SelectedItems
///
/// SelectedItems Attached Dependency Property
///
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
typeof(ListBoxSelectedItemsAttachedProperty),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnSelectedItemsChanged)));

public static IList GetSelectedItems(DependencyObject d)
{
return (IList)d.GetValue(SelectedItemsProperty);
}

public static void SetSelectedItems(DependencyObject d, IList value)
{
d.SetValue(SelectedItemsProperty, value);
}

private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListBox miLb = (ListBox)d;
miLb.SelectionChanged += listBox_SelectionChanged;
}

private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox miLg = (ListBox)sender;
//Get list box's selected items.
IEnumerable miDgSelectedItems = miLg.SelectedItems;
//Get list from model
IList ModelSelectedItems = GetSelectedItems(miLg);

//Update the model
ModelSelectedItems.Clear();

if (miLg.SelectedItems != null)
{
foreach (var item in miLg.SelectedItems)
ModelSelectedItems.Add(item);
}
SetSelectedItems(miLg, ModelSelectedItems);
}
#endregion
}


And in the axml it is used in this way, for example in a Listbox:



Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding MyPropertyInViewModel}"


Attached Behavoir:



public class SelectedItemsBehavior : Behavior<MultiSelector>
{
protected override void OnAttached()
{
AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= AssociatedObjectSelectionChanged;
}

void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<object> selectedItemList = AssociatedObject.SelectedItems.Cast<object>().ToList();
ObservableCollection<object> selectedItems = new ObservableCollection<object>(selectedItemList);
SelectedItems = selectedItems;
}
public ObservableCollection<object> SelectedItems
{
get { return (ObservableCollection<object>)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems"
, typeof(ObservableCollection<object>)
, typeof(SelectedItemsBehavior)
,
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}


And in the axml is used in this way, for example in a DataGrid:



<i:Interaction.Behaviors>
<Behaviors:SelectedItemsBehavior SelectedItems="{Binding MyPropertyInViewModel}" />
</i:Interaction.Behaviors>


But Really I don't know the differences between an attached property and an attached behavior, and what is the best option to get the SelectedItems in the ViewModel.



Thanks.










share|improve this question














I have to options to get the SelectedItems in the ViewModel.



A attached property like this:



public class ListBoxSelectedItemsAttachedProperty
{
#region SelectedItems
///
/// SelectedItems Attached Dependency Property
///
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
typeof(ListBoxSelectedItemsAttachedProperty),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnSelectedItemsChanged)));

public static IList GetSelectedItems(DependencyObject d)
{
return (IList)d.GetValue(SelectedItemsProperty);
}

public static void SetSelectedItems(DependencyObject d, IList value)
{
d.SetValue(SelectedItemsProperty, value);
}

private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListBox miLb = (ListBox)d;
miLb.SelectionChanged += listBox_SelectionChanged;
}

private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox miLg = (ListBox)sender;
//Get list box's selected items.
IEnumerable miDgSelectedItems = miLg.SelectedItems;
//Get list from model
IList ModelSelectedItems = GetSelectedItems(miLg);

//Update the model
ModelSelectedItems.Clear();

if (miLg.SelectedItems != null)
{
foreach (var item in miLg.SelectedItems)
ModelSelectedItems.Add(item);
}
SetSelectedItems(miLg, ModelSelectedItems);
}
#endregion
}


And in the axml it is used in this way, for example in a Listbox:



Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding MyPropertyInViewModel}"


Attached Behavoir:



public class SelectedItemsBehavior : Behavior<MultiSelector>
{
protected override void OnAttached()
{
AssociatedObject.SelectionChanged += AssociatedObjectSelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= AssociatedObjectSelectionChanged;
}

void AssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<object> selectedItemList = AssociatedObject.SelectedItems.Cast<object>().ToList();
ObservableCollection<object> selectedItems = new ObservableCollection<object>(selectedItemList);
SelectedItems = selectedItems;
}
public ObservableCollection<object> SelectedItems
{
get { return (ObservableCollection<object>)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems"
, typeof(ObservableCollection<object>)
, typeof(SelectedItemsBehavior)
,
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}


And in the axml is used in this way, for example in a DataGrid:



<i:Interaction.Behaviors>
<Behaviors:SelectedItemsBehavior SelectedItems="{Binding MyPropertyInViewModel}" />
</i:Interaction.Behaviors>


But Really I don't know the differences between an attached property and an attached behavior, and what is the best option to get the SelectedItems in the ViewModel.



Thanks.







c# wpf attached-properties attachedbehaviors






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 20:20









Álvaro GarcíaÁlvaro García

6,3411862116




6,3411862116













  • Why not just bind the SelectedItems property of the control to a view model property? That's what MVVM is all about, and is waaaay simpler than either of the options you have shown (which are both overkill for the requirement)

    – Avrohom Yisroel
    Nov 17 '18 at 23:01



















  • Why not just bind the SelectedItems property of the control to a view model property? That's what MVVM is all about, and is waaaay simpler than either of the options you have shown (which are both overkill for the requirement)

    – Avrohom Yisroel
    Nov 17 '18 at 23:01

















Why not just bind the SelectedItems property of the control to a view model property? That's what MVVM is all about, and is waaaay simpler than either of the options you have shown (which are both overkill for the requirement)

– Avrohom Yisroel
Nov 17 '18 at 23:01





Why not just bind the SelectedItems property of the control to a view model property? That's what MVVM is all about, and is waaaay simpler than either of the options you have shown (which are both overkill for the requirement)

– Avrohom Yisroel
Nov 17 '18 at 23:01












1 Answer
1






active

oldest

votes


















1














There are basically two different ways of implementing attached behaviours in WPF. You could either create an attached property and apply a PropertyChangedCallback to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.



The other way is to create a class that dervies from System.Windows.Interactivity.Behavior<T>. This is commonly referred to as a "Blend" behaviour and it provides a better way of encapsulating the functionality of a behaviour compared to creating an attached property with a callback. Blend behaviours are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend and they also offer an easy and clean way to attach to and release event handlers using the OnAttached and OnDetaching methods. The main drawback is that you cannot apply these kind of behaviours in style setters.



So if you need to be able to attach your behaviour in a Style, use an attached property. Otherwise I would prefer to use a Blend behaviour. There is an example of how to bind to read-only properties available on this blog if you are interested.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355213%2fattachedproperty-vs-behavior-to-get-selecteditems-in-the-viewmodel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There are basically two different ways of implementing attached behaviours in WPF. You could either create an attached property and apply a PropertyChangedCallback to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.



    The other way is to create a class that dervies from System.Windows.Interactivity.Behavior<T>. This is commonly referred to as a "Blend" behaviour and it provides a better way of encapsulating the functionality of a behaviour compared to creating an attached property with a callback. Blend behaviours are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend and they also offer an easy and clean way to attach to and release event handlers using the OnAttached and OnDetaching methods. The main drawback is that you cannot apply these kind of behaviours in style setters.



    So if you need to be able to attach your behaviour in a Style, use an attached property. Otherwise I would prefer to use a Blend behaviour. There is an example of how to bind to read-only properties available on this blog if you are interested.






    share|improve this answer




























      1














      There are basically two different ways of implementing attached behaviours in WPF. You could either create an attached property and apply a PropertyChangedCallback to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.



      The other way is to create a class that dervies from System.Windows.Interactivity.Behavior<T>. This is commonly referred to as a "Blend" behaviour and it provides a better way of encapsulating the functionality of a behaviour compared to creating an attached property with a callback. Blend behaviours are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend and they also offer an easy and clean way to attach to and release event handlers using the OnAttached and OnDetaching methods. The main drawback is that you cannot apply these kind of behaviours in style setters.



      So if you need to be able to attach your behaviour in a Style, use an attached property. Otherwise I would prefer to use a Blend behaviour. There is an example of how to bind to read-only properties available on this blog if you are interested.






      share|improve this answer


























        1












        1








        1







        There are basically two different ways of implementing attached behaviours in WPF. You could either create an attached property and apply a PropertyChangedCallback to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.



        The other way is to create a class that dervies from System.Windows.Interactivity.Behavior<T>. This is commonly referred to as a "Blend" behaviour and it provides a better way of encapsulating the functionality of a behaviour compared to creating an attached property with a callback. Blend behaviours are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend and they also offer an easy and clean way to attach to and release event handlers using the OnAttached and OnDetaching methods. The main drawback is that you cannot apply these kind of behaviours in style setters.



        So if you need to be able to attach your behaviour in a Style, use an attached property. Otherwise I would prefer to use a Blend behaviour. There is an example of how to bind to read-only properties available on this blog if you are interested.






        share|improve this answer













        There are basically two different ways of implementing attached behaviours in WPF. You could either create an attached property and apply a PropertyChangedCallback to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.



        The other way is to create a class that dervies from System.Windows.Interactivity.Behavior<T>. This is commonly referred to as a "Blend" behaviour and it provides a better way of encapsulating the functionality of a behaviour compared to creating an attached property with a callback. Blend behaviours are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend and they also offer an easy and clean way to attach to and release event handlers using the OnAttached and OnDetaching methods. The main drawback is that you cannot apply these kind of behaviours in style setters.



        So if you need to be able to attach your behaviour in a Style, use an attached property. Otherwise I would prefer to use a Blend behaviour. There is an example of how to bind to read-only properties available on this blog if you are interested.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 13:38









        mm8mm8

        82.8k81831




        82.8k81831






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355213%2fattachedproperty-vs-behavior-to-get-selecteditems-in-the-viewmodel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)