Click here to Skip to main content
15,890,123 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: checking processes Pin
Abhinav S3-Dec-10 23:22
Abhinav S3-Dec-10 23:22 
QuestionStandard Button Layout Pin
PaulPrice3-Dec-10 0:20
PaulPrice3-Dec-10 0:20 
AnswerRe: Standard Button Layout Pin
Abhinav S3-Dec-10 1:07
Abhinav S3-Dec-10 1:07 
GeneralRe: Standard Button Layout Pin
PaulPrice3-Dec-10 2:04
PaulPrice3-Dec-10 2:04 
AnswerRe: Standard Button Layout Pin
Pete O'Hanlon3-Dec-10 1:16
mvePete O'Hanlon3-Dec-10 1:16 
JokeRe: Standard Button Layout Pin
PaulPrice3-Dec-10 2:02
PaulPrice3-Dec-10 2:02 
GeneralRe: Standard Button Layout Pin
Pete O'Hanlon3-Dec-10 4:35
mvePete O'Hanlon3-Dec-10 4:35 
QuestionDependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice2-Dec-10 3:13
PaulPrice2-Dec-10 3:13 
Afternoon Guys and Gals,

I am trying to learn how to implement Custom Controls in WPF for use in a reusable controls library because I am not happy with the current implementation as our current method does not play well with MVVM.

As an experiment I am trying to create a custom control that has two ListViews and two buttons, the idea being to allow a user to bind to SelectedItems and DeSelectItems properties in the view. These will populate the two list boxes from properties in a viewmodel. The buttons simply allowing the user to select an item and move it from one ListView to the other, ie selected / not selected.

The problem I have is that when I bind to the SelectedItems and DeSelectedItems in xaml, neither dependancy property in the custom control actually get populates, ie the lists stay empty.

I believe my mistake is related to Binding Source, something I do not understand so well

The Custom Control Source - (The Test application is implemented as MVVM)

namespace CustomControlLibrary
{
    [TemplatePart(Name = DualListControl.ElementNotSelectedList, Type = typeof(ListView))]
    [TemplatePart(Name = DualListControl.ElementSelectedList, Type = typeof(ListView))]
    [TemplatePart(Name = DualListControl.ElementSelectButton, Type = typeof(Button))]
    [TemplatePart(Name = DualListControl.ElementDeselectButton, Type = typeof(Button))]
    public class DualListControl : Control
    {
        const string ElementNotSelectedList = "ElementNotSelectedList";
        const string ElementSelectedList = "ElementSelectedList";
        const string ElementSelectButton = "ElementSelectButton";
        const string ElementDeselectButton = "ElementDeselectButton";

        ListView _NotSelectedList, _SelectedList;
        Button _SelectButton, _DeselectButton;
        RelayCommand _SelectItemCommand, _DeSelectItemCommand;

        static DualListControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DualListControl), new FrameworkPropertyMetadata(typeof(DualListControl)));
        }

        public DualListControl()
        {
            _SelectItemCommand = new RelayCommand(p => SelectItemCommandExecute(), p => SelectItemCommandCanExecute);
            _DeSelectItemCommand = new RelayCommand(p => DeSelectItemCommandExecute(), p => DeSelectItemCommandCanExecute);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _NotSelectedList = GetTemplateChild(ElementNotSelectedList) as ListView;
            
            _SelectedList = GetTemplateChild(ElementSelectedList) as ListView;

            _SelectButton = GetTemplateChild(ElementSelectButton) as Button;
            _SelectButton.Command = _SelectItemCommand as ICommand;

            _DeselectButton = GetTemplateChild(ElementDeselectButton) as Button;
            _DeselectButton.Command = _DeSelectItemCommand as ICommand;
        }

        bool SelectItemCommandCanExecute { get { return true; } }
        bool DeSelectItemCommandCanExecute { get { return true; } }

        void SelectItemCommandExecute()
        {
            if (!SelectItemCommandCanExecute)
                return;

        }
        void DeSelectItemCommandExecute()
        {
            if (!DeSelectItemCommandCanExecute)
                return;

            SelectedItems.Add(_NotSelectedList.SelectedItem);
            DeSelectedItems.Remove(_NotSelectedList.SelectedItem);

            int i = 0;
        }

        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("SelectedItems", typeof(ObservableCollection<object>), typeof(DualListControl), new UIPropertyMetadata(new ObservableCollection<object>()));
        public static readonly DependencyProperty DeSelectedItemsProperty =
            DependencyProperty.Register("DeSelectedItems", typeof(ObservableCollection<object>), typeof(DualListControl), new UIPropertyMetadata(new ObservableCollection<object>()));

        public ObservableCollection<object> SelectedItems
        {
            get { return (ObservableCollection<object>)GetValue(SelectedItemsProperty); }
            set { SetValue(SelectedItemsProperty, value); }
        }
        public ObservableCollection<object> DeSelectedItems
        {
            get { return (ObservableCollection<object>)GetValue(DeSelectedItemsProperty); }
            set { SetValue(DeSelectedItemsProperty, value); }
        }
    }
}


The Generic.xaml

<Style TargetType="{x:Type local:DualListControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DualListControl}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <ListView Grid.Column="0" Name="ElementNotSelectedList" ItemsSource="{Binding Path=DeSelectedItems}"/>
                    <Grid Grid.Column="1">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Button Grid.Row="1" Name="ElementSelectButton" Margin="5" Content="&lt;"/>
                        <Button Grid.Row="2" Name="ElementDeselectButton" Margin="5" Content="&gt;"/>
                    </Grid>
                    <ListView Grid.Column="2" Name="ElementSelectedList" ItemsSource="{Binding Path=SelectedItems}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


The View

<UserControl x:Class="TestApplication.Views.MainAppView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:CustomControls="clr-namespace:CustomControlLibrary;assembly=CustomControlLibrary"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <CustomControls:DualListControl SelectedItems="{Binding SelectedItemsA}" DeSelectedItems="{Binding DeslectedItemsA}"/>
    </Grid>
</UserControl>


The Modelview

namespace TestApplication.ViewModels
{
    public class MainAppViewModel : BaseViewModel
    {
        ObservableCollection<object> _SelectedItems = new ObservableCollection<object>(),
                                    _DeSelectedItems = new ObservableCollection<object>();

        public ObservableCollection<object> SelectedItemsA
        {
            get { return _SelectedItems; }
        }
        public ObservableCollection<object> DeSelectedItemsA
        {
            get { return _DeSelectedItems; }
        }

        public MainAppViewModel()
        {
            _DeSelectedItems.Add("One");
            _DeSelectedItems.Add("Two");
            _DeSelectedItems.Add("Three");
            _DeSelectedItems.Add("Four");
        }
    }
}


Appreciate anyone who can help to identify my mistake(s), 5's in the lounge as a possible bribe?
Just racking up the postings

AnswerRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
Ian Shlasko2-Dec-10 3:55
Ian Shlasko2-Dec-10 3:55 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice2-Dec-10 4:36
PaulPrice2-Dec-10 4:36 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
Ian Shlasko2-Dec-10 6:18
Ian Shlasko2-Dec-10 6:18 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice2-Dec-10 6:34
PaulPrice2-Dec-10 6:34 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
Ian Shlasko2-Dec-10 9:56
Ian Shlasko2-Dec-10 9:56 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
SledgeHammer012-Dec-10 10:05
SledgeHammer012-Dec-10 10:05 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
Ian Shlasko2-Dec-10 10:59
Ian Shlasko2-Dec-10 10:59 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
SledgeHammer012-Dec-10 11:58
SledgeHammer012-Dec-10 11:58 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice3-Dec-10 0:11
PaulPrice3-Dec-10 0:11 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice3-Dec-10 0:05
PaulPrice3-Dec-10 0:05 
AnswerRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
SledgeHammer012-Dec-10 6:35
SledgeHammer012-Dec-10 6:35 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice2-Dec-10 7:05
PaulPrice2-Dec-10 7:05 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
SledgeHammer012-Dec-10 10:04
SledgeHammer012-Dec-10 10:04 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
PaulPrice2-Dec-10 23:59
PaulPrice2-Dec-10 23:59 
GeneralRe: DependancyProperty Confusion with Custom Controls and ModelView Pin
SledgeHammer013-Dec-10 14:33
SledgeHammer013-Dec-10 14:33 
Questionvs 2010 settings Pin
arkiboys1-Dec-10 21:43
arkiboys1-Dec-10 21:43 
AnswerRe: vs 2010 settings Pin
Abhinav S1-Dec-10 22:34
Abhinav S1-Dec-10 22:34 

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.