WPF Grid - Checkboxes within a ListBox - Community Credit Forums
in

    Community Credit Forums

WPF Grid - Checkboxes within a ListBox

Last post 05-28-2008 8:23 AM by Happyarimaan. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 05-24-2008 6:38 AM

    WPF Grid - Checkboxes within a ListBox

    Iam developing an application in VS2008 (with ComponentOne Grid) coding in VB, using which the user can select the columns to be displayed in a grid by clicking on the Checkboxes corresponding to the columns to be displayed within the listbox.

    I have run the application but it is giving two errors.

    i)Could not create an instance of type'StaticExtension'
    ii)Cannot find the type 'VisibilityToBoolConverter'
    I am sending the relevant XAML code as well as the VisibilityToBoolConverter.vb code
    <XAML>---code---
    <Window x:Class="NEW_WpfApplication_20_5_08.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c1grid="clr-namespace:C1.WPF.C1DataGrid;assembly=C1.WPF.C1DataGrid"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:NEW_WpfApplication_20_5_08"
        Title="Window1" Height="300" Width="300" WindowState="Maximized">
        <Window.Resources>
            <DataTemplate x:Key="lbItemTemplate">
                <StackPanel Orientation="Horizontal">
                    <!-- DataContext is a Column -->
                    <CheckBox IsChecked="{Binding Visibility ,
                        Converter={x:Static local:VisibilityToBoolConverter.Default},
                                    Mode=TwoWay}"/>
        <TextBlock Text="{Binding Caption}"/>
       </StackPanel>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" MinHeight="82" />
                <RowDefinition/>
            </Grid.RowDefinitions>
            <ListBox ItemsSource="{Binding ActualOrdinaryColumns, ElementName=dataGrid1}"
       ItemTemplate="{StaticResource lbItemTemplate}" Height="60" VerticalAlignment="Top"
                     SelectionMode="Multiple" />
            <c1grid:C1DataGrid Name="dataGrid1" Margin="0,64,0,0" Grid.RowSpan="2">
                <c1grid:C1DataGrid.Columns>
                    <c1grid:Column ColumnName="C1" />
                    <c1grid:Column ColumnName="Column2" />
                    <c1grid:Column ColumnName="Column3" />
                    <c1grid:Column ColumnName="Column4" />
                </c1grid:C1DataGrid.Columns>
            </c1grid:C1DataGrid>
        </Grid>
    </Window>

    </XAML>
    <VisibilityToBoolConverter.vb>---code---
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Data
    Imports System.Windows
    Imports System.Globalization

    Namespace NEW_WpfApplication_20_5_08
      Public Class VisibilityToBoolConverter
        Implements IValueConverter

        Public Shared ReadOnly [Default] As New VisibilityToBoolConverter()

        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
          If TypeOf value Is Visibility Then
            Return CType(value, Visibility) = Visibility.Visible
          Else
            Return Binding.DoNothing
          End If
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
          If TypeOf value Is Boolean Then
            Return If(CBool(value), Visibility.Visible, Visibility.Collapsed)
          Else
            Return Binding.DoNothing
          End If
        End Function
      End Class
    End Namespace
    </VisibilityToBoolConverter.vb>

    Could somebody point out the discrepancies.

    Regards ......... Happyarimaan
    Filed under:
  • 05-25-2008 6:35 AM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    The converter must be a StaticResource and not code.   Change your Windows.Resources to look like the below code.  You can also remove the "Public Shared ReadOnly [Default] As New VisibilityToBoolConverter()" from your converter, WPF won't be using it.

    <Window.Resources>

      <local:VisibilityToBoolConverter x:Key=visibilityToBoolConverter" />

      <DataTemplate x:Key="lbItemTemplate">
        <StackPanel Orientation="Horizontal">
          <!-- DataContext is a Column -->
          <CheckBox IsChecked="{Binding Visibility, Converter={StaticResource visibilityToBoolConverter}, Mode=TwoWay}"/>
          <TextBlock Text="{Binding Caption}"/>
       </StackPanel>
     </DataTemplate>
    </Window.Resources>

    Cheers,

    Karl

    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
    Filed under:
  • 05-27-2008 1:32 AM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    Karl,  I have changed the code as instructed and ran the program. Still there are two errors shown :

    i)The tag 'VisibilityToBoolConverter' does not exist in XML namespace 'clr-namespace:NEW_WpfApplication_20_5_08'

    ii)The type 'local:VisibilityToBoolConverter' was not found.Verify that you are not missing and assembly reference and that all assembly references have been built.

    Iam getting confused.Hmm

    Regards ......... Happyarimaan
  • 05-27-2008 8:24 AM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    You have two namespaces defined.

    Remove the "Namespace NEW_WpfApplication_20_5_08" from the converter .vb file.  Just use the default namespace defined in your properties window of the project.

     Rerun the application.

    Cheers,

    Karl

    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
  • 05-27-2008 11:52 PM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    Thanks Karl It worked. You have an ans. for everything.
    Regards ......... Happyarimaan
  • 05-28-2008 7:18 AM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    Well you don't become a CodeProjectMVP and Microsoft MVP for nothing. Smile

    Kindest regards,
      David Silverlight

    Support Community Credit by giving some love to StupidCubicle.com


  • 05-28-2008 7:41 AM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    You two are too kind.

    Cheers,

    Karl

    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
  • 05-28-2008 8:23 AM In reply to

    Re: WPF Grid - Checkboxes within a ListBox

    Nope you are being MODEST
    Regards ......... Happyarimaan
Page 1 of 1 (8 items)
Powered by Community Server (Commercial Edition), by Telligent Systems