visibility of form sections based on selected index of DataGrid
-
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
-
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
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
-
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
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}}">
-
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}}">
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:
-
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:
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
-
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
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:
-
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
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
-
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
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???
-
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???
Hmm CodeProject editor must have added the space - there's no space in my code here! :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hmm CodeProject editor must have added the space - there's no space in my code here! :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
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>
-
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>
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: