Click here to Skip to main content
15,887,083 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: MVVM: best way to wire up this Command scenario? Pin
Pete O'Hanlon24-Jun-11 10:37
mvePete O'Hanlon24-Jun-11 10:37 
GeneralRe: MVVM: best way to wire up this Command scenario? Pin
SledgeHammer0124-Jun-11 10:42
SledgeHammer0124-Jun-11 10:42 
QuestionProblem with WPF and removing a row from a datagrid Pin
Member 297299224-Jun-11 10:05
Member 297299224-Jun-11 10:05 
QuestionSearching for a common base class for StackPanel and Control Pin
cethie23-Jun-11 22:15
cethie23-Jun-11 22:15 
AnswerRe: Searching for a common base class for StackPanel and Control Pin
Pete O'Hanlon23-Jun-11 22:51
mvePete O'Hanlon23-Jun-11 22:51 
AnswerRe: Searching for a common base class for StackPanel and Control Pin
Abhinav S23-Jun-11 23:44
Abhinav S23-Jun-11 23:44 
AnswerRe: Searching for a common base class for StackPanel and Control Pin
Mark Salsbery24-Jun-11 5:58
Mark Salsbery24-Jun-11 5:58 
QuestionWPF MVVM ListBox Basics Pin
Member 450279423-Jun-11 2:13
Member 450279423-Jun-11 2:13 
Hi everyone, I'm new to WPF. I'd appreciate your advice on what should be simple functionality.

I'm building a wizard and am basing my code on the following example:
Creating an Internationalized Wizard in WPF[^]

Rather than using an Items Control with a Data Template of CheckBoxes I want to use an Items Control with a Data Template of ListBoxItems all wrapped in a ListBox.

I have created the following XAML

<ListBox>
                        <ItemsControl
                           ItemsSource="{Binding Path=AvailablePitchbookChartFamily}"
                           Width="100">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <ListBoxItem
                                        Content="{Binding Path=DisplayName}"
                                        IsSelected="{Binding Path=IsSelected}"
                                    />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ListBox>


The collection AvailablePitchbookChartFamily is of Type OptionViewModel the full implementation of this class is displayed at the end of this post. The OptionViewModel class allows me to assign display properties and event handlers to each item in the collection.

The problem I am having is that when I have bound the collection of OptionViewModel to my Items Control the ListBox that wraps the Items Control does not function. What I am trying to achieve is that when you select an item in ListBox the PropertyChanged event handler for the ListBoxItem would fire allowing me to update the properties of the object I am modelling in the business logic. However currently all of the required ListBox items are displayed in the ListBox but they are not selectable and the PropertyChanged event is not fired when you attempt to select them.

I'm guessing that the OptionViewModel type in its current form is not going to work with a ListBox but I would be very happy if someone can prove me wrong.

Here's the relevant code behind for the ViewModel.

Private _objAvailablePitchbookChartFamily As ReadOnlyCollection(Of OptionViewModel(Of enumPitchbookChartFamily))

Public ReadOnly Property AvailablePitchbookChartFamily() As ReadOnlyCollection(Of OptionViewModel(Of enumPitchbookChartFamily))
        Get

            If _objAvailablePitchbookChartFamily Is Nothing Then
                CreateAvailablePitchbookChartFamily()
            End If

            Return _objAvailablePitchbookChartFamily
        End Get
    End Property

Private Sub CreateAvailablePitchbookChartFamily()

        '   We can't bind directly to the enum as we need to support multiple languages
        '   Map the enum to the resource file string here
        Dim obj As New List(Of OptionViewModel(Of enumPitchbookChartFamily))
        With obj
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Area, enumPitchbookChartFamily.Area))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Bar, enumPitchbookChartFamily.Bar))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Bubble, enumPitchbookChartFamily.Bubble))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Column, enumPitchbookChartFamily.Column))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Line, enumPitchbookChartFamily.Line))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Pie, enumPitchbookChartFamily.Pie))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Scatter, enumPitchbookChartFamily.Scatter))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Stock, enumPitchbookChartFamily.Stock))
            .Add(New OptionViewModel(Of enumPitchbookChartFamily)(My.Resources.Strings.ChartFamily_Waterfall, enumPitchbookChartFamily.Waterfall))
        End With


        For Each objOption As OptionViewModel(Of enumPitchbookChartFamily) In obj

            '   Set the default value assigned in in Model for display in the view
            If Me.PitchbookChart.ChartFamily = objOption.GetValue Then
                objOption.IsSelected = True
            End If

            AddHandler objOption.PropertyChanged, AddressOf OnPitchbookChartFamilySelectionChanged
        Next

        obj.Sort()
        _objAvailablePitchbookChartFamily = New ReadOnlyCollection(Of OptionViewModel(Of enumPitchbookChartFamily))(obj)
    End Sub


Here is the full implementation of OptionViewModel Class

Imports System.ComponentModel

Public Class OptionViewModel(Of TValue)
    Implements INotifyPropertyChanged
    Implements IComparable(Of OptionViewModel(Of TValue))

#Region " Declarations "

    Private Const UNSET_SORT_VALUE As Integer = Integer.MinValue
    Private _bolIsSelected As Boolean = False
    Private _intSortValue As Integer = Integer.MinValue
    Private _objValue As TValue
    Private _strDisplayName As String = String.Empty

#End Region

#Region " Properties "

    Public ReadOnly Property DisplayName() As String
        Get
            Return _strDisplayName
        End Get
    End Property

    Public Property IsSelected() As Boolean
        Get
            Return _bolIsSelected
        End Get
        Set(ByVal Value As Boolean)
            _bolIsSelected = Value
            OnPropertyChanged("IsSelected")
        End Set
    End Property

    Public ReadOnly Property SortValue() As Integer
        Get
            Return _intSortValue
        End Get
    End Property

#End Region

#Region " Constructors "

    Public Sub New(ByVal strDisplayName As String, ByVal objValue As TValue)
        _strDisplayName = strDisplayName
        _objValue = objValue
    End Sub

    Public Sub New(ByVal strDisplayName As String, ByVal objValue As TValue, ByVal intSortValue As Integer)
        _strDisplayName = strDisplayName
        _objValue = objValue
        _intSortValue = intSortValue
    End Sub

#End Region

#Region " Events "

    Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

#End Region

#Region " Methods "

    Public Function CompareTo(ByVal other As OptionViewModel(Of TValue)) As Integer Implements System.IComparable(Of OptionViewModel(Of TValue)).CompareTo

        If other Is Nothing Then
            Return -1

        ElseIf Me.SortValue = UNSET_SORT_VALUE AndAlso other.SortValue = UNSET_SORT_VALUE Then
            Return Me.DisplayName.CompareTo(other.DisplayName)

        ElseIf Me.SortValue <> UNSET_SORT_VALUE AndAlso other.SortValue <> UNSET_SORT_VALUE Then
            Return Me.SortValue.CompareTo(other.SortValue)

        ElseIf Me.SortValue <> UNSET_SORT_VALUE AndAlso other.SortValue = UNSET_SORT_VALUE Then
            Return -1

        Else
            Return +1
        End If

    End Function

    Public Function GetValue() As TValue
        Return _objValue
    End Function

    Protected Sub OnPropertyChanged(ByVal strPropertyName As String)

        Dim handler As PropertyChangedEventHandler = Me.PropertyChangedEvent

        If handler IsNot Nothing Then
            handler.Invoke(Me, New PropertyChangedEventArgs(strPropertyName))
        End If

    End Sub

#End Region

End Class

AnswerRe: WPF MVVM ListBox Basics Pin
SledgeHammer0123-Jun-11 9:55
SledgeHammer0123-Jun-11 9:55 
GeneralRe: WPF MVVM ListBox Basics Pin
Member 450279424-Jun-11 1:36
Member 450279424-Jun-11 1:36 
GeneralRe: WPF MVVM ListBox Basics Pin
SledgeHammer0124-Jun-11 4:59
SledgeHammer0124-Jun-11 4:59 
QuestionMVVM: TreeViewItem double click -> command? Pin
SledgeHammer0122-Jun-11 12:44
SledgeHammer0122-Jun-11 12:44 
AnswerRe: MVVM: TreeViewItem double click -> command? Pin
Pete O'Hanlon22-Jun-11 17:24
mvePete O'Hanlon22-Jun-11 17:24 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
SledgeHammer0122-Jun-11 17:38
SledgeHammer0122-Jun-11 17:38 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Pete O'Hanlon22-Jun-11 21:09
mvePete O'Hanlon22-Jun-11 21:09 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
SledgeHammer0123-Jun-11 6:49
SledgeHammer0123-Jun-11 6:49 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
SledgeHammer0123-Jun-11 6:57
SledgeHammer0123-Jun-11 6:57 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Pete O'Hanlon23-Jun-11 7:01
mvePete O'Hanlon23-Jun-11 7:01 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
SledgeHammer0123-Jun-11 7:13
SledgeHammer0123-Jun-11 7:13 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Pete O'Hanlon23-Jun-11 7:14
mvePete O'Hanlon23-Jun-11 7:14 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Mycroft Holmes23-Jun-11 12:04
professionalMycroft Holmes23-Jun-11 12:04 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
SledgeHammer0123-Jun-11 12:50
SledgeHammer0123-Jun-11 12:50 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Mycroft Holmes23-Jun-11 14:47
professionalMycroft Holmes23-Jun-11 14:47 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Pete O'Hanlon23-Jun-11 20:20
mvePete O'Hanlon23-Jun-11 20:20 
GeneralRe: MVVM: TreeViewItem double click -> command? Pin
Pete O'Hanlon23-Jun-11 20:19
mvePete O'Hanlon23-Jun-11 20:19 

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.