Results 1 to 6 of 6

Thread: WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    46

    WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

    I'm looking for a way, if possible, to make values in a listbox "disabled" meaning they get greyed out, but not the entire list. I've got a listbox, that depending on its selected item, populates values into another listbox. What I'd like to do is have something similar to the following code so that if there are no values to populate into the second listbox, then the line is greyed out.


    Code:
    SQL.CommandText = "SELECT COUNT * FROM Topics WHERE Question =" & ProductID
    SQLResult = SQL.ExecuteNonQuery()
    Count = SQLResult
    
    If Count =0 Then
    
    lstProducts.selectedRow(2).enabled = false
    
    End IF
    obviously the last line (lstproducts.selectedrow.enabled) is a bogus line, but I'm looking for something like that, even if it's applying a template to the box or something.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

    There are probably several ways to do it - personally I would use a Converter (might not be suitable for you but without knowing more about what you are doing its hard to say). It might be a bit hard to explain this but I've done it successfully in the past with relative ease.
    Basically what you would do is have a property on your listbox items (assuming you are adding instances of your own class to the listbox rather than just strings) that specifies whether or not this item should be greyed out and you would pass this value in via data binding to the Foreground property of whatever it is that you want to be greyed out (a TextBlock or whatever it is that is displaying the item) and tell it to use a converter. The converter then takes the value of the property, which in this case would just need to be a boolean, and if it is True it returns a black Brush object (which is what will get assigned to the Foreground property of our TextBlock) and if it is False then it returns a grey Brush The great thing about this is that you don't have to just limit it to greying out the text like you would in Winforms, you can use this method to affect any properties you like and depending on how elaborate your DataTemplate is for these items there could be a much 'cooler' way of showing that the item is disabled
    I'll put together a basic example anyway as that probably wasn't the best explanation in the world...

    Oh and if you don't want to have to determine if the item needs to be disabled before it is added to the listbox then you could probably just implement INotifyPropertyChanged on that property I talked about creating and then when the user selects an item you execute your SQL statement and assign the relevant value to the property for the selected item. Personally I would think it is better to grey out the items before the user clicks on them though.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

    OK I've put together a very simple example that just uses a property named Disabled on each item to determine what colour the text should be. Like I said you could do much more with this but its just an example

    First of all here's a screenshot:



    and here is the full XAML for that window (I'll post the VB code behind in the next post) :

    Code:
    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:ExampleConverter x:Key="ExampleConverterResource" />
            <DataTemplate x:Key="ExampleDataTemplate">
                <Grid>
                    <TextBlock Text="{Binding Path=ExampleTitle}" Foreground="{Binding Path=Disabled,Converter={StaticResource ExampleConverterResource}}" />
                </Grid>
            </DataTemplate>
        </Window.Resources>
        <Grid>
             <ListBox Name="ExampleListBox" Margin="10" ItemTemplate="{StaticResource ExampleDataTemplate}" />
        </Grid>
    </Window>
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

    and here's all of the VB code

    First of all this is the ExampleConverter class that we call from the XAML to actually convert the Disabled property value into a colour so that we can assign it to the Foreground property of the Textblock in the data template:
    vb Code:
    1. <ValueConversion(GetType(Boolean), GetType(Brush))> _ '<-- This tells the converter what type of value to expect for the input and output of this conversion
    2. Public Class ExampleConverter : Implements IValueConverter
    3.  
    4.     Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    5.         'This method gets called when the DataTemplate populates the TextBlock's Foreground property and the "value" object passed
    6.         'in is the value of our Disabled property for this item as that is what we specified for the Foreground property data binding in the XAML
    7.         Dim BooleanValue As Boolean = CBool(value)
    8.         If BooleanValue = True Then
    9.             Return Brushes.Gray 'Return a grey coloured brush if the Disabled property was set to True
    10.         Else
    11.             Return Brushes.Black 'Else return a black coloured brush
    12.         End If
    13.     End Function
    14.  
    15.     Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    16.         Return Nothing 'Don't care as we won't be converting it back
    17.     End Function
    18.  
    19. End Class

    Now here's the code in the window itself:
    vb Code:
    1. Class MainWindow
    2.  
    3.     Private ObjectList As New List(Of ExampleItemClass) 'This is the list that the listbox will be bound to
    4.  
    5.     Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    6.         'Add items to a list and set the Disabled property on some of them
    7.         ObjectList.Add(New ExampleItemClass With {.ExampleTitle = "Example enabled item"})
    8.         ObjectList.Add(New ExampleItemClass With {.ExampleTitle = "Example disabled item", .Disabled = True})
    9.         ObjectList.Add(New ExampleItemClass With {.ExampleTitle = "Another example enabled item"})
    10.         ObjectList.Add(New ExampleItemClass With {.ExampleTitle = "and another example enabled item"})
    11.         ObjectList.Add(New ExampleItemClass With {.ExampleTitle = "Another example disabled item", .Disabled = True})
    12.  
    13.         'Set the listbox item source to our list of items
    14.         ExampleListBox.ItemsSource = ObjectList
    15.     End Sub
    16.  
    17. End Class

    and our basic item class that we add instances of to the listbox, that has the Disabled property we use in the converter:
    vb Code:
    1. Public Class ExampleItemClass
    2.  
    3.     Private _ExampleTitle As String = String.Empty
    4.     Public Property ExampleTitle() As String
    5.         Get
    6.             Return _ExampleTitle
    7.         End Get
    8.         Set(ByVal value As String)
    9.             _ExampleTitle = value
    10.         End Set
    11.     End Property
    12.  
    13.     Private _Disabled As Boolean = False
    14.     Public Property Disabled() As Boolean
    15.         Get
    16.             Return _Disabled
    17.         End Get
    18.         Set(ByVal value As Boolean)
    19.             _Disabled = value
    20.         End Set
    21.     End Property
    22.  
    23. End Class

    Hope that helps and let me know if you have any questions Oh and I know you might be thinking what the hell, all that just to grey out an item in a listbox... but remember that this can do much more than that and this is just a basic example. There may well be a better and simpler way to simply grey out the text as you wanted but I can't think of anything off the top of my head and this is a very useful 'trick' to learn
    Just as a quick example of the kind of other things you can do with this technique, here's a screenshot of a chat application I made ages ago - the colour of each user and also the image for each user is selected automatically via a converter based on their current status (away, busy etc) :

    Last edited by chris128; Nov 19th, 2010 at 06:21 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

    The problem with the ValueConverter approach is that you're baking the UI look into the ValueConverter. This is far from ideal. Using a trigger as part of the DataTemplate when templating the ExampleItemClass removes the need for the converter and puts all the code relating to the look of the item in one place. Any further changes required for a "disabled" item are then simply additional Setter elements inside the DataTrigger, and not additional ValueConverter classes:

    Code:
    <Window x:Class="WpfTestBed.Window3"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:WpfTestBed="clr-namespace:WpfTestBed"
            Title="Window3" Height="300" Width="300">
        <Window.Resources>
            <DataTemplate DataType="{x:Type WpfTestBed:ExampleItemClass}">
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Disabled}" Value="True">
                        <Setter Property="Control.Foreground" Value="Gray" />
                    </DataTrigger>
                </DataTemplate.Triggers>
                <TextBlock Text="{Binding ExampleTitle}" />
            </DataTemplate>
        </Window.Resources>
        
        <Grid>
            <ListBox>
                <ListBox.Items>
                    <WpfTestBed:ExampleItemClass ExampleTitle="Not Disabled" Disabled="False" />
                    <WpfTestBed:ExampleItemClass ExampleTitle="Disabled" Disabled="True" />
                </ListBox.Items>
            </ListBox>
        </Grid>
    </Window>

  6. #6
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    Re: WPF VB - .NET 3.5/4 : Anyway to "disable" a value in a listbox?

    Hey Chris128, Can you post the source code for the "ProtoChat" status

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width