views:

150

answers:

3

am usina text block in usercontrol, but am sending value to textblock from other form, when i pass some value it viewed in textblock, but i need to convert the number to text. so i used converter in textblock. but its not working

 <TextBlock Height="21" Name="txtStatus" Width="65" Background="Bisque" TextAlignment="Center" Text="{Binding Path=hM1,Converter={StaticResource TextConvert},Mode=OneWay}"/>

converter class

class TextConvert : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (value != null)
        {
            if (value.ToString() == "1")
            {
                return value = "Good";

            }
            if (value.ToString() == "0")
            {
                return value = "NIL";

            }

       }
        return value = "";
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (string)value;
    }

}

is it right? whats wrong in it??

+3  A: 

ok I think I know what the problem is - let see if I can define it for you :)

in your xaml file where you want to use TextConvert, define Resource for it (unless you are doing it already, then I haven't a clue why its not working)

    <Grid.Resources>
        <Shared:TextConvert x:Key="TextConvertKey" />
    </Grid.Resources>

shared being the xmlns ofcourse.

Then in the textbox use it like:

Text="{Binding Path=hM1,Converter={StaticResource TextConvertKey},Mode=OneWay}"/>

EDIT:

If you set a breakpoint in the converter class, does the debugger go in there?????

EDIT 2:

am using like this voodoo

local:HealthTextConvert x:Key="TextConvert"

This is absolutely wrong. How can you Call it HealthTextConvert when the converter name is TextConvert???

it should be

local:TextConvert x:Key="whateverKeyNameYouWant"

and

in the textbox is should be

Text="{Binding Path=hM1,Converter={StaticResource whateverKeyNameYouWant},Mode=OneWay}"
VoodooChild
i have already added it to my grid resources..but its stil convert class is not working
deep
let me see what hM1 looks like?
VoodooChild
hM1 is a variable,it may be 0 or 1
deep
If you set a breakpoint in the converter class, does the debugger go in there?????
VoodooChild
no, i ichecked that too, its does not going to that break point.
deep
I would check the output window in VS to see what kind of errors you are getting...The are exceptions being thrown by SL
VoodooChild
none error am getting, evrythng works well with out any exception. i just added converter class to the textblock,but the output remain same, only the converter class is not working, i can get the output without conversion and without exception
deep
are you using the "{StaticResource TextConvertKey}"? like I said in my solution. You can't use the converter name here (i.e. TextConvert)
VoodooChild
am using like this voodoo <UserControl.Resources> <local:HealthTextConvert x:Key="TextConvert"/> </UserControl.Resources>
deep
check my answer I edited it again
VoodooChild
Dont forget to specify the local namespace in the top of your xaml ;)
Arcturus
no i did correctly, but still not convert not working vodoo.. don know why
deep
what is the name of the file in which Converter resides in and namespace? what is your xmlns for local???
VoodooChild
A: 

Try by removing "Path" in the below line

Text="{Binding Path=hM1,Converter={StaticResource TextConvert},Mode=OneWay}".

Sometimes it works without "path" :).

Also look into the output window(Alt+Cntl+O)...to see where the issue is.

Anish
but its not working anish..?
deep
output window doesnt show any exception.
deep
Deep ..did u get the answer ?
Anish
Namespace shud be like this - xmlns:Model="clr-namespace:WpfControlLibrary2.ViewModel" And Class in that namespace shud be used as below - <ObjectDataProvider x:Key="Datas" ObjectType="{x:Type ViewModel:UserControlViewModel}"></ObjectDataProvider>
Anish
using ObjectDataProvider...u can have the instance of a class as a static resource
Anish
what does this have to do with anything????
VoodooChild
I'm sorry that was a mistake from my part :(Please look at the following link - http://blogs.msdn.com/b/bencon/archive/2006/05/10/594886.aspx
Anish
A: 

I can see immediately a problem with your converter definition.

class TextConvert : IValueConverter
{
    ...

Should be declared public to be able to use it as a resource.

public class TextConvert : IValueConverter
{
    ...

Also, its not a good thing to be doing this...

return value = "Good";

...

return value = "NIL";

It should just be (even though it will not matter if you leave it, just bad programming =P):

return "Good";

...

return "Nill";
Tri Q