Click here to Skip to main content
15,906,816 members
Home / Discussions / WPF
   

WPF

 
QuestionSilverlight3, VS2008 Pin
DuttaSandip4U30-Sep-11 23:08
DuttaSandip4U30-Sep-11 23:08 
AnswerRe: Silverlight3, VS2008 Pin
Abhinav S30-Sep-11 23:24
Abhinav S30-Sep-11 23:24 
QuestionSilverlight slider - binding lost when value is out-of-range Pin
Wjousts29-Sep-11 8:41
Wjousts29-Sep-11 8:41 
AnswerRe: Silverlight slider - binding lost when value is out-of-range Pin
killabyte29-Sep-11 17:23
killabyte29-Sep-11 17:23 
GeneralRe: Silverlight slider - binding lost when value is out-of-range Pin
BobJanova30-Sep-11 4:45
BobJanova30-Sep-11 4:45 
GeneralRe: Silverlight slider - binding lost when value is out-of-range Pin
Wjousts30-Sep-11 10:08
Wjousts30-Sep-11 10:08 
GeneralRe: Silverlight slider - binding lost when value is out-of-range Pin
Wjousts30-Sep-11 9:55
Wjousts30-Sep-11 9:55 
QuestionWPF combobox selecteditem issue Pin
PretzelB29-Sep-11 5:34
PretzelB29-Sep-11 5:34 
I had a working example of a combobox jumping to the correct selecteditem based on the choice of another combobox but I can't seem to make it work with a very simple example. I'll try to keep the code to a minimum but I don't see why a choice in "product" will not cause the correct "category" to be selected. This same code seemed to work fine when working with an EF data model based on the NW database.

Main window
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="1200">


    <Grid Width="900" HorizontalAlignment="Left" DataContext="{Binding}">
        <Grid.RowDefinitions>
            <RowDefinition Height="44*" />
            <RowDefinition Height="44*" />
            <RowDefinition Height="44*" />
            <RowDefinition Height="256*" />
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Margin="5,0,0,0" FlowDirection="LeftToRight" Orientation="Horizontal" VerticalAlignment="Top"  HorizontalAlignment="Left" >
            <TextBlock Text="Select a Product" Margin="5"></TextBlock>
            <ComboBox Height="28" Name="cmbProducts2" Width="150" Margin="3"
                        IsEditable="False"
                        ItemsSource="{Binding Path=myProductList,  Mode=OneWay,  UpdateSourceTrigger=PropertyChanged}"
                        SelectedItem="{Binding mySelectedProduct}"
                        >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="{Binding Path=ProductName}" />
                            <TextBlock Text=" (" />
                            <TextBlock Text="{Binding Path=ProductID}" />
                            <TextBlock Text=") " />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ComboBox>
        </StackPanel>

        <StackPanel Grid.Row="1" Margin="5,0,0,0" FlowDirection="LeftToRight" Orientation="Horizontal" VerticalAlignment="Top"  HorizontalAlignment="Left" >
            <TextBlock Text="Category Should Auto Select" Margin="5"></TextBlock>
            <ComboBox Height="28" Name="cmbCategory2" Width="150" Margin="3" IsEditable="False"
                        ItemsSource="{Binding Path=myCategoryList,  Mode=OneWay,  UpdateSourceTrigger=PropertyChanged}"
                        SelectedItem="{Binding mySelectedCategory}"
                        >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="{Binding Path=CategoryName}" />
                            <TextBlock Text=" (" />
                            <TextBlock Text="{Binding Path=CategoryID}" />
                            <TextBlock Text=") " />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ComboBox>

        </StackPanel>
        <StackPanel Grid.Row="2" Margin="5,0,0,0" FlowDirection="LeftToRight" Orientation="Horizontal" VerticalAlignment="Top"  HorizontalAlignment="Left" >
            <TextBlock Text="mySelectedCategory.CategoryName=" />
            <TextBlock Text="{Binding mySelectedCategory.CategoryName}" Foreground="#FFFC0000"/>
            <TextBlock Text="  mySelectedCategory.CategoryID=" />
            <TextBlock Text="{Binding mySelectedCategory.CategoryID}" Foreground="#FFFC0000"/>
        </StackPanel>

        <StackPanel Grid.Row="3" Margin="5,0,0,0" FlowDirection="LeftToRight" Orientation="Horizontal" VerticalAlignment="Top"  HorizontalAlignment="Left" >
            <TextBlock Text="mySelectedProduct.myCategory.CategoryName=" />
            <TextBlock Text="{Binding mySelectedProduct.myCategory.CategoryName}" Foreground="#FFFC0000" ForceCursor="True"></TextBlock>
            <TextBlock Text="  mySelectedProduct.myCategory.CategoryID=" />
            <TextBlock Text="{Binding mySelectedProduct.myCategory.CategoryID}" Foreground="#FFFC0000"/>
        </StackPanel>

    </Grid>

</Window>


Data repository
XML
Imports System.Collections.ObjectModel
Public Class myDataRepository
    Private _mycateorylist As ObservableCollection(Of myCategory)
    Public Sub New()
        _mycateorylist = New ObservableCollection(Of myCategory)
        _mycateorylist = GetMyCategories()
    End Sub
    Public Function GetMyProducts() As ObservableCollection(Of myProduct)
        Dim a As New myProduct()
        Dim b As New myProduct
        Dim c As New myProduct
        Dim d As New myProduct
        a.ProductID = 1
        a.ProductName = "tire"
        a.CategoryID = 1
        b.ProductID = 2
        b.ProductName = "wipers"
        b.CategoryID = 1
        c.ProductID = 3
        c.ProductName = "tv"
        c.CategoryID = 2
        d.ProductID = 4
        d.ProductName = "radio"
        d.CategoryID = 2
        Dim list As New List(Of myProduct)
        list.Add(a)
        list.Add(b)
        list.Add(c)
        list.Add(d)
        Return New ObservableCollection(Of myProduct)(list)
    End Function
    Public Function GetMyCategories() As ObservableCollection(Of myCategory)
        Dim a As New myCategory
        Dim b As New myCategory
        a.CategoryID = 1
        a.CategoryName = "auto"
        b.CategoryID = 2
        b.CategoryName = "electronics"
        Dim list As New List(Of myCategory)
        list.Add(a)
        list.Add(b)
        Return New ObservableCollection(Of myCategory)(list)
    End Function
    Public Function GetMyCategory(ByVal id As Int32) As myCategory
        Dim results = From c In _mycateorylist
                Where c.CategoryID = id
                Select c
        Dim rs As IEnumerable(Of myCategory) = From x In results.AsEnumerable
                      Select New myCategory With
                             {
                                 .CategoryID = x.CategoryID,
                                 .CategoryName = x.CategoryName
                             }
        Return rs.FirstOrDefault
    End Function
End Class
Public Class myCategory
#Region "Primitive Properties"
    Public Property CategoryID() As Global.System.Int32
        Get
            Return _CategoryID
        End Get
        Set(value As Global.System.Int32)
            _CategoryID = value
        End Set
    End Property
    Private _CategoryID As Global.System.Int32
    Public Property CategoryName() As Global.System.String
        Get
            Return _CategoryName
        End Get
        Set(value As Global.System.String)
            _CategoryName = value
        End Set
    End Property
    Private _CategoryName As Global.System.String
#End Region
End Class
Public Class myProduct
#Region "Primitive Properties"
    Public Property ProductID() As Global.System.Int32
        Get
            Return _ProductID
        End Get
        Set(value As Global.System.Int32)
            _ProductID = value
        End Set
    End Property
    Private _ProductID As Global.System.Int32
    Public Property ProductName() As Global.System.String
        Get
            Return _ProductName
        End Get
        Set(value As Global.System.String)
            _ProductName = value
        End Set
    End Property
    Private _ProductName As Global.System.String
    Public Property CategoryID() As Nullable(Of Global.System.Int32)
        Get
            Return _CategoryID
        End Get
        Set(value As Nullable(Of Global.System.Int32))
            _CategoryID = value
        End Set
    End Property
    Private _CategoryID As Nullable(Of Global.System.Int32)
    Private _mycategory As myCategory
    Public Property myCategory As myCategory
        Get
            Return _mycategory
        End Get
        Set(value As myCategory)
            _mycategory = value
        End Set
    End Property
#End Region
End Class



View model
XML
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class MainWindowViewModel
    Implements INotifyPropertyChanged
    Public Property myProductList As ObservableCollection(Of myProduct)
    Public Property myCategoryList As ObservableCollection(Of myCategory)
    Private Property _context As myDataRepository
    Public Sub New(ByVal db As myDataRepository)
        _context = db
        myProductList = _context.GetMyProducts()
        myCategoryList = _context.GetMyCategories()
    End Sub
    Private Property _myselectedproduct As myProduct
    Public Property mySelectedProduct As myProduct
        Get
            Return _myselectedproduct
        End Get
        Set(value As myProduct)
            _myselectedproduct = value
            mySelectedProduct.myCategory = _context.GetMyCategory(mySelectedProduct.CategoryID)
            mySelectedCategory = mySelectedProduct.myCategory
            OnPropertyChanged(mySelectedProduct, New System.ComponentModel.PropertyChangedEventArgs("mySelectedProduct"))
        End Set
    End Property
    Private Property _myselectedcategory As myCategory
    Public Property mySelectedCategory As myCategory
        Get
            Return _myselectedcategory
        End Get
        Set(value As myCategory)
            _myselectedcategory = value
            OnPropertyChanged(mySelectedCategory, New System.ComponentModel.PropertyChangedEventArgs("mySelectedCategory"))
        End Set
    End Property
#Region "INotifyPropertyChanged Members"
    Private Sub OnPropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        Dim propertyName As String = e.PropertyName
        Me.NotifyPropertyChanged(propertyName)
    End Sub
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    Private Sub NotifyPropertyChanged(ByVal propertyName As String)
        If Me.PropertyChangedEvent IsNot Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End If
    End Sub
#End Region
End Class

AnswerRe: WPF combobox selecteditem issue Pin
Ian Shlasko29-Sep-11 5:57
Ian Shlasko29-Sep-11 5:57 
GeneralRe: WPF combobox selecteditem issue Pin
PretzelB29-Sep-11 6:19
PretzelB29-Sep-11 6:19 
GeneralRe: WPF combobox selecteditem issue Pin
Ian Shlasko29-Sep-11 8:43
Ian Shlasko29-Sep-11 8:43 
GeneralRe: WPF combobox selecteditem issue Pin
PretzelB29-Sep-11 9:31
PretzelB29-Sep-11 9:31 
GeneralRe: WPF combobox selecteditem issue Pin
Ian Shlasko29-Sep-11 10:32
Ian Shlasko29-Sep-11 10:32 
AnswerRe: WPF combobox selecteditem issue Pin
Simon Bang Terkildsen29-Sep-11 8:07
Simon Bang Terkildsen29-Sep-11 8:07 
GeneralRe: WPF combobox selecteditem issue Pin
PretzelB29-Sep-11 9:27
PretzelB29-Sep-11 9:27 
QuestionHow to use WPF to implement multi-pen drawing? Pin
rehuo28-Sep-11 21:58
rehuo28-Sep-11 21:58 
AnswerRe: How to use WPF to implement multi-pen drawing? Pin
rehuo5-Oct-11 20:02
rehuo5-Oct-11 20:02 
QuestionHow to get WM_Gesture messages in WPF4.0 touch aware application? Pin
manil kumar28-Sep-11 21:15
manil kumar28-Sep-11 21:15 
QuestionListBox Binding Pin
Samir.Sh28-Sep-11 20:46
Samir.Sh28-Sep-11 20:46 
GeneralRe: ListBox Binding Pin
Samir.Sh29-Sep-11 20:16
Samir.Sh29-Sep-11 20:16 
QuestionObsolete Examples Pin
michaelbarb28-Sep-11 8:53
michaelbarb28-Sep-11 8:53 
AnswerRe: Obsolete Examples Pin
AnnieMacD28-Sep-11 10:05
AnnieMacD28-Sep-11 10:05 
GeneralRe: Obsolete Examples Pin
michaelbarb28-Sep-11 10:21
michaelbarb28-Sep-11 10:21 
GeneralRe: Obsolete Examples Pin
SledgeHammer0128-Sep-11 10:46
SledgeHammer0128-Sep-11 10:46 
GeneralRe: Obsolete Examples Pin
michaelbarb29-Sep-11 8:13
michaelbarb29-Sep-11 8:13 

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.