Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. visibility of form sections based on selected index of DataGrid

visibility of form sections based on selected index of DataGrid

Scheduled Pinned Locked Moved WPF
databasehelptutorial
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Michael J Eber

    We are writing a Silverlight 4.0 application which is actually a conversion of ASP to this application. In one block of forms we have a datagrid at the top of the form. The remainder of the form (each grouped in sections) can only be visible when the datagrid's selected index is > 0. So I know I have to bind the Visibility attribute and I know I have to use a BooleanToVisibilityConverter. What I'm not sure about is that I cannot seem to find just the right statement to get addressability to the datagrid and its' properties leastwise how to do the comparison. I'm thinking I may have to write my own Converter to do the comparison logic but I still have to know how to define the datagrid as the object being passed to the converter. Any help would be greatly appreciated!!! Thanks much, Michael

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #2

    How about a IndexGreaterThanZeroToVisibilityConverter...

    public class IndexGreaterThanZeroToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((int)value > 0) ? Visibility.Visible : Visibility.Collapsed;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    (Note: ListBox used here for simplicity...works the same with a DataGrid)

    <UserControl.Resources>
        <local:IndexGreaterThanZeroToVisibilityConverter x:Key="IndexGreaterThanZeroToVisibilityConverter" />
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="\*" />
        </Grid.RowDefinitions>
        <ListBox Name="listBox1" Grid.Row="0" Height="100" >
            <ListBoxItem Content="Item 1" />
            <ListBoxItem Content="Item 2" />
            <ListBoxItem Content="Item 3" />
        </ListBox>
        <Grid Grid.Row="1" **Visibility="{Binding ElementName=listBox1, Path=SelectedIndex, Mode=OneWay, Converter={StaticResource IndexGreaterThanZeroToVisibilityConverter}}"** >
            <TextBlock Text="This area is visibile!" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </Grid>
    

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    modified on Thursday, May 19, 2011 11:43 PM

    M 1 Reply Last reply
    0
    • M Mark Salsbery

      How about a IndexGreaterThanZeroToVisibilityConverter...

      public class IndexGreaterThanZeroToVisibilityConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return ((int)value > 0) ? Visibility.Visible : Visibility.Collapsed;
          }
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
      

      (Note: ListBox used here for simplicity...works the same with a DataGrid)

      <UserControl.Resources>
          <local:IndexGreaterThanZeroToVisibilityConverter x:Key="IndexGreaterThanZeroToVisibilityConverter" />
      </UserControl.Resources>
      
      <Grid x:Name="LayoutRoot" Background="White">
          <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
              <RowDefinition Height="\*" />
          </Grid.RowDefinitions>
          <ListBox Name="listBox1" Grid.Row="0" Height="100" >
              <ListBoxItem Content="Item 1" />
              <ListBoxItem Content="Item 2" />
              <ListBoxItem Content="Item 3" />
          </ListBox>
          <Grid Grid.Row="1" **Visibility="{Binding ElementName=listBox1, Path=SelectedIndex, Mode=OneWay, Converter={StaticResource IndexGreaterThanZeroToVisibilityConverter}}"** >
              <TextBlock Text="This area is visibile!" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" />
          </Grid>
      </Grid>
      

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      modified on Thursday, May 19, 2011 11:43 PM

      M Offline
      M Offline
      Michael J Eber
      wrote on last edited by
      #3

      Hey Mark! You're okay for a C++ guy. :laugh: So I implemented my convert. check Then I implemented the binding. check Added the namespace for local. check Then did local: my converter was visible and got it set with a key. check Ran the solution and got this error at compile time. Error 1 The tag 'DataGridVisibilityConverter' does not exist in XML namespace 'clr-namespace:CNRPGlobal.Converters; assembly=GlobalLibraryDefinitions'. So how is it I can type local: and see my class library. Yet when I compile XAML cannot? I confirmed it is public. Here is a snippet of what I've got:

      xmlns:local="clr-namespace:CNRPGlobal.Converters; assembly=GlobalLibraryDefinitions"
      xmlns:sys="clr-namespace:System;assembly=mscorlib">
      
      <UserControl.Resources>
          <ResourceDictionary x:Key="styles" Source="/GlobalLibraryDefinitions;component/Themes/Generic.xaml" />
          <local:DataGridVisibilityConverter x:Key="VisibilityConverter" />
      </UserControl.Resources>
      

      And the consumption of the converter:

                      <cnrp:FormSection x:Name="designSection" Title="Design Section" DataContext="{Binding CurrentDesign, Mode=TwoWay}"
                                        Visibility="{Binding ElementName=dgDesigns, Path=SelectedIndex, Mode=OneWay, Converter={StaticResource VisibilityConverter}}">
      
      M 1 Reply Last reply
      0
      • M Michael J Eber

        Hey Mark! You're okay for a C++ guy. :laugh: So I implemented my convert. check Then I implemented the binding. check Added the namespace for local. check Then did local: my converter was visible and got it set with a key. check Ran the solution and got this error at compile time. Error 1 The tag 'DataGridVisibilityConverter' does not exist in XML namespace 'clr-namespace:CNRPGlobal.Converters; assembly=GlobalLibraryDefinitions'. So how is it I can type local: and see my class library. Yet when I compile XAML cannot? I confirmed it is public. Here is a snippet of what I've got:

        xmlns:local="clr-namespace:CNRPGlobal.Converters; assembly=GlobalLibraryDefinitions"
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
        
        <UserControl.Resources>
            <ResourceDictionary x:Key="styles" Source="/GlobalLibraryDefinitions;component/Themes/Generic.xaml" />
            <local:DataGridVisibilityConverter x:Key="VisibilityConverter" />
        </UserControl.Resources>
        

        And the consumption of the converter:

                        <cnrp:FormSection x:Name="designSection" Title="Design Section" DataContext="{Binding CurrentDesign, Mode=TwoWay}"
                                          Visibility="{Binding ElementName=dgDesigns, Path=SelectedIndex, Mode=OneWay, Converter={StaticResource VisibilityConverter}}">
        
        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #4

        From what you've shown I can' see the problem. Try a rebuild? The namespace and assembly are correct and you have a reference to that assembly? Also, "local" maybe not a good name for a namespace in a separate assembly :)

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M 1 Reply Last reply
        0
        • M Mark Salsbery

          From what you've shown I can' see the problem. Try a rebuild? The namespace and assembly are correct and you have a reference to that assembly? Also, "local" maybe not a good name for a namespace in a separate assembly :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          M Offline
          M Offline
          Michael J Eber
          wrote on last edited by
          #5

          Mark, OMG -- the initial problem was I had a space between the namespace and the assembly name But now I'm getting a runtime error Local values are not allowed in resource dictionary with Source set [Line: 14 Position: 46] It is pointing at my styles object! X|

          modified on Friday, May 20, 2011 2:15 PM

          M 2 Replies Last reply
          0
          • M Michael J Eber

            Mark, OMG -- the initial problem was I had a space between the namespace and the assembly name But now I'm getting a runtime error Local values are not allowed in resource dictionary with Source set [Line: 14 Position: 46] It is pointing at my styles object! X|

            modified on Friday, May 20, 2011 2:15 PM

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #6

            Patterns are for programmers....the compiler doesn't know anything about MVVM. :) If there's a class called DataGridVisibilityConverter in a namespace CNRPGlobal.Converters in an assembly GlobalLibraryDefinitions then it should work.

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            1 Reply Last reply
            0
            • M Michael J Eber

              Mark, OMG -- the initial problem was I had a space between the namespace and the assembly name But now I'm getting a runtime error Local values are not allowed in resource dictionary with Source set [Line: 14 Position: 46] It is pointing at my styles object! X|

              modified on Friday, May 20, 2011 2:15 PM

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #7

              Try a USerControl from scratch, totally stripped down..does this compile??

              <UserControl x:Class="SilverlightTester.ConverterTestPage"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:gspace="clr-namespace:CNRPGlobal.Converters;assembly=GlobalLibraryDefinitions"
              mc:Ignorable="d"
              d:DesignHeight="300" d:DesignWidth="400">

              <UserControl.Resources>
                  <gspace:DataGridVisibilityConverter x:Key="VisibilityConverter" />
              </UserControl.Resources>
              
              <Grid x:Name="LayoutRoot" Background="White">
              </Grid>
              

              </UserControl>

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              modified on Friday, May 20, 2011 2:25 PM

              M 1 Reply Last reply
              0
              • M Mark Salsbery

                Try a USerControl from scratch, totally stripped down..does this compile??

                <UserControl x:Class="SilverlightTester.ConverterTestPage"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:gspace="clr-namespace:CNRPGlobal.Converters;assembly=GlobalLibraryDefinitions"
                mc:Ignorable="d"
                d:DesignHeight="300" d:DesignWidth="400">

                <UserControl.Resources>
                    <gspace:DataGridVisibilityConverter x:Key="VisibilityConverter" />
                </UserControl.Resources>
                
                <Grid x:Name="LayoutRoot" Background="White">
                </Grid>
                

                </UserControl>

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                modified on Friday, May 20, 2011 2:25 PM

                M Offline
                M Offline
                Michael J Eber
                wrote on last edited by
                #8

                got past the compile issue. It was the space just before assembly:!!! Now I just have to figure out my runtime issues with the resource dictionary. X| It doesn't like having that resource along with a ResourceDictionary entry! WTF???

                M 1 Reply Last reply
                0
                • M Michael J Eber

                  got past the compile issue. It was the space just before assembly:!!! Now I just have to figure out my runtime issues with the resource dictionary. X| It doesn't like having that resource along with a ResourceDictionary entry! WTF???

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #9

                  Hmm CodeProject editor must have added the space - there's no space in my code here! :)

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  M 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Hmm CodeProject editor must have added the space - there's no space in my code here! :)

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    M Offline
                    M Offline
                    Michael J Eber
                    wrote on last edited by
                    #10

                    WOOHOO!!! Got a clean compile AND a good XAML parse. Here is the final results I had to implement!

                    xmlns:g="clr-namespace:CNRPGlobal.Converters;assembly=GlobalLibraryDefinitions"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
                    
                    <UserControl.Resources>
                        <ResourceDictionary>
                            <ResourceDictionary.MergedDictionaries>
                                <ResourceDictionary Source="/GlobalLibraryDefinitions;component/Themes/Generic.xaml" />
                                <ResourceDictionary>
                                    <g:DataGridVisibilityConverter x:Key="VisibilityConverter" />
                                </ResourceDictionary>
                            </ResourceDictionary.MergedDictionaries>
                        </ResourceDictionary>
                    
                    </UserControl.Resources>
                    
                    M 1 Reply Last reply
                    0
                    • M Michael J Eber

                      WOOHOO!!! Got a clean compile AND a good XAML parse. Here is the final results I had to implement!

                      xmlns:g="clr-namespace:CNRPGlobal.Converters;assembly=GlobalLibraryDefinitions"
                      xmlns:sys="clr-namespace:System;assembly=mscorlib">
                      
                      <UserControl.Resources>
                          <ResourceDictionary>
                              <ResourceDictionary.MergedDictionaries>
                                  <ResourceDictionary Source="/GlobalLibraryDefinitions;component/Themes/Generic.xaml" />
                                  <ResourceDictionary>
                                      <g:DataGridVisibilityConverter x:Key="VisibilityConverter" />
                                  </ResourceDictionary>
                              </ResourceDictionary.MergedDictionaries>
                          </ResourceDictionary>
                      
                      </UserControl.Resources>
                      
                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #11

                      Awesome! Was just about to send this...

                      <UserControl.Resources>
                          <ResourceDictionary>
                              <ResourceDictionary.MergedDictionaries>
                                  <ResourceDictionary Source="/GlobalLibraryDefinitions;component/Themes/Generic.xaml" />
                              </ResourceDictionary.MergedDictionaries>
                              <converters:IndexGreaterThanZeroToVisibilityConverter x:Key="IndexGreaterThanZeroToVisibilityConverter" />
                          </ResourceDictionary>
                      </UserControl.Resources>
                      

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups