WPF Textblock Convert Issue

am usina text block in usercontrol, but I send the textblock value from another form when I pass some value that it looked at in the text block, but I need to convert the number to text. so I used the converter in the text block. 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;
    }

}

Correctly? What's bad about it?

+3
source share
3 answers

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

in your xaml file where you want to use TextConvert, define a resource for it (if you do not, then I do not know why it does not work)

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

shared as xmlns of course.

Then in the text box use it like:

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

EDIT:

, .

2:

am,

local: HealthTextConvert x: Key = "TextConvert"

. HealthTextConvert, TextConvert???

local:TextConvert x:Key="whateverKeyNameYouWant"

Text="{Binding Path=hM1,Converter={StaticResource whateverKeyNameYouWant},Mode=OneWay}"
+3

.

class TextConvert : IValueConverter
{
    ...

, .

public class TextConvert : IValueConverter
{
    ...

, ...

return value = "Good";

...

return value = "NIL";

( , , = P):

return "Good";

...

return "Nill";
+1

" "

= "{ = hM1, = {StaticResource TextConvert}, Mode = OneWay}".

"":).

Also view the output window (Alt + Cntl + O) ... to find out where the problem is.

0
source

Source: https://habr.com/ru/post/1748096/


All Articles