Skip to main content

Microsoft Silverlight

Answered Question What is wrong with the Converter?RSS Feed

(0)

codebased
codebased

Participant

Participant

806 points

360 Posts

What is wrong with the Converter?

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 :(

 

"If I've answered your query then please mark it as "Answered".

preishuber
preishuber

Contributor

Contributor

3570 points

655 Posts

Re: What is wrong with the Converter?

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?

-Hannes

http://www.preishuber.net http://weblogs.asp.net/hpreishuber

codebased
codebased

Participant

Participant

806 points

360 Posts

Re: What is wrong with the Converter?

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 :(

 

"If I've answered your query then please mark it as "Answered".

codebased
codebased

Participant

Participant

806 points

360 Posts

Re: What is wrong with the Converter?

Can someone help me here please?

"If I've answered your query then please mark it as "Answered".

TomBeeby
TomBeeby

Participant

Participant

1151 points

188 Posts

Answered Question

Re: What is wrong with the Converter?

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

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities