Skip to main content
Home Forums Silverlight Programming Programming with .NET - General What is wrong with the Converter?
4 replies. Latest Post by TomBeeby on November 28, 2008.
(0)
codebased
Participant
806 points
360 Posts
11-25-2008 1:09 AM |
I got a data grid where data is coming through the Web service.
I got <CellTemplate /> with the edit button and I want to set the visibility on the basis of if I can update or not. The flag is coming from the web service, so I got like this:
List<Employee> employees = e.Result;
employeesDataGrid.ItemsSource = ...;
Now in the button I wrote like this:
<Button x:Name="EditClient" Margin="2,0" DataContext="{Binding ClientID}" Click="EditClient_Click" Style="{StaticResource GreyBTN}" Content="Edit" Visibility="{Binding CanUpdate, Converter={StaticResource ToVisibility}}" />
Here you can see that I've got ao binding to the visibility property and got a converter that converts the true false value to System.Windows.Visiblity.
I got a conversion code like this:
public class BooleanToVisibilityConverter : IValueConverter { #region IValueConverter Members /// <summary> /// Modifies the source data before passing it to the /// target for display in the // UI. /// </summary> /// <param name="value">The source data being passed to the target.</param> /// <param name="targetType">The System.Type of data expected by the target dependency property.</param> /// <param name="parameter">An optional parameter to be used in the converter logic.</param> /// <param name="culture">The culture of the conversion.</param> /// <returns>The value to be passed to the target dependency property.</returns> /// <remarks>NOTE: AT PRESENT WE ARE CONVERTING VALUE INTO OF TYPE DATE ONLY!! /// format: dd/MM/yyyy</remarks> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return null; } bool? v = (bool)value; if (v == true) { return (object)System.Windows.Visibility.Visible; } else { return (object)System.Windows.Visibility.Collapsed; } }
What is the solution of this? Anyone? please!
If I do a debugging, and make a debug cursor to this class, it does not get called at all.
PLEASE HELP :(
preishuber
Contributor
3570 points
655 Posts
11-25-2008 1:51 AM |
if its not called and not error, it can be a problem in the staticresource declaration of the converter. It's case sensisitve, Can you post that part of xaml?
11-25-2008 8:09 AM |
The BooleanToVisibilityConverter class is in the common sl dll so i've declated like this in app.xaml
<common:BooleanToVisibilityConverter x:key="ToVisibility"/>
common is the namespace pointing to the dll.
btw I've got another field for the date and there also I am using another converter which is working. The only difference i can see here is that one is binding for content property where as this one for visibility property, will it make a difference i dont know :(
11-27-2008 6:22 PM |
TomBeeby
1151 points
188 Posts
11-28-2008 12:20 AM |
The converter class works fine. it is not the problem (as long as you also implement ConvertBack)
look at:
1. how you are referencing the converter... try something like this:
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <loc:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </Grid.Resources> <TextBlock Text="Hello" Visibility="{Binding Test, Converter={StaticResource BooleanToVisibilityConverter}}"/> </Grid>
where loc points to the namespace where the converter resides
2. make sure that the input data is what you expect and that the datacontext of the Button is correct
To repeat: the converter is fine, the issue is in the XAML