How dinamically set property in xaml

Hi, is there any way to set the color of a label by the xaml?
The text property is already binded to a variable, and i'ld like to set the color as:

if(int.parse(text)< 0) ValueColor=Color.Red;
else ValueColor=Color.Green;

Could i do that?

Posts

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    You are looking for creating a Converter like on this page.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    If its a one-time need I'd probably use a Trigger instead of a converter. That keeps the condition inside the XAML.
    For example: If TypedUserPassword is empty

    If its a condition that you would re-use throughout your app such as CollectionIsEmpty then a converter is a good choice.

  • osharko-kunosharko-kun ITMember ✭✭

    Hi, @ClintStLaurent
    In real, i don't know when the variable is assigned.
    The line in the xaml is that:
    <my:SingolaRigaCollapsed Even="False" Height="40" HeaderText="Something" ValueText="{Binding RendimentoAlLordoProventi}" ValueColor="Green"/>
    So, i don't think a coul use a if into the xaml line.

  • osharko-kunosharko-kun ITMember ✭✭

    I just discovered that i have to use this property for a lot of Text... so, could I create a xaml function and then call it for all the text i need?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    XAML doesn't have functions... And you don't 'call' anything in XAML. XAML is not code. It doesn't execute.

    i have to use this property for a lot of Text

    That happens some times. For example you want the version number, or company name to be defined in one place, but used in several places like the 'About' view, the footer of all the pages and so on.

    You can define the property in the main app class. That is available to everything within the app.

  • osharko-kunosharko-kun ITMember ✭✭

    @ClintStLaurent said:
    XAML doesn't have functions... And you don't 'call' anything in XAML. XAML is not code. It doesn't execute.

    i have to use this property for a lot of Text

    That happens some times. For example you want the version number, or company name to be defined in one place, but used in several places like the 'About' view, the footer of all the pages and so on.

    You can define the property in the main app class. That is available to everything within the app.

    so, for changing the color i have to use the converter?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You just said you needed to use a text property throughout the app. I answered that. Put it in the main app class. What you do with it from there is up to you. I don't know what that has to do with a converter for colors.

    Try to stick to one issue at a time or you confuse those trying to help you.

  • osharko-kunosharko-kun ITMember ✭✭
    edited August 2017

    @AdamMeaney said:
    You are looking for creating a Converter like on this page.

    `

    In ValueColor, if I want use a converter, how should i do?
    I just create the converter where the function do this:
    return value.ToString().Contains("-") ? Color.Red : Color.Green; (if is a negative number, red color else green).

    How should i use the converter now?

    Obviously this line make the program to crash:
    ValueColor="{Binding trystring, Converter={StaticResource ValueToColorConverter}}"
    I haven't understand if i can bind a string and then convert it, or i must bind a Color.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm going to repeat my beleif that a converter is not the right choice here.
    You can mark it all up in XAML and have the UI layer take care of the Ui - probably through a Trigger
    https://forums.xamarin.com/discussion/comment/293719#Comment_293719

    If you really want more explanation of how to make a converter this should help
    https://redpillxamarin.com/2016/12/23/203-now-for-the-left-foot/

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I agree with Clint that some circumstances make more sense as a trigger if they are easy enough to code that way.

    Read up on the Xamarin guides for both and pick the one that fits best.

    I am still trying to get myself to use Triggers for more things, as I didn't understand how they worked and just wrote converters for the longest while.

  • osharko-kunosharko-kun ITMember ✭✭

    @ClintStLaurent said:
    I'm going to repeat my beleif that a converter is not the right choice here.
    You can mark it all up in XAML and have the UI layer take care of the Ui - probably through a Trigger
    https://forums.xamarin.com/discussion/comment/293719#Comment_293719

    If you really want more explanation of how to make a converter this should help
    https://redpillxamarin.com/2016/12/23/203-now-for-the-left-foot/

    The trigger should be the better way even if in the code is really difficult to find the variable? (from the xaml i can take the same variable taken from the text binding, from the code i don't know where to place the trigger

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    If you decide that a trigger doesn't fit your needs, my first link on converters shows how to apply it in XAML.

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:XamlSamples;assembly=XamlSamples"
                 x:Class="XamlSamples.ListViewDemoPage"
                 Title="ListView Demo Page">
    
      <ContentPage.Resources>
        <ResourceDictionary>
          <OnPlatform x:Key="boxSize"
                      x:TypeArguments="x:Double"
                      iOS="50"
                      Android="50"
                      WinPhone="75" />
    
          <!-- This is only an issue on the iPhone; Android and
               WinPhone auto size the row height to the contents. -->
          <OnPlatform x:Key="rowHeight"
                      x:TypeArguments="x:Int32"
                      iOS="60"
                      Android="60"
                      WinPhone="85" />
    
          <local:DoubleToIntConverter x:Key="intConverter" />
    
        </ResourceDictionary>
      </ContentPage.Resources>
    

    Get the right namespace and make an element like

    Then

    <Label Text="{Binding Color.G,
                                       Converter={StaticResource intConverter},
                                       ConverterParameter=255,
                                       StringFormat=', G={0:X2}'}" />
    
  • osharko-kunosharko-kun ITMember ✭✭

    @AdamMeaney said:
    If you decide that a trigger doesn't fit your needs, my first link on converters shows how to apply it in XAML.

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:XamlSamples;assembly=XamlSamples"
                 x:Class="XamlSamples.ListViewDemoPage"
                 Title="ListView Demo Page">
    
      <ContentPage.Resources>
        <ResourceDictionary>
          <OnPlatform x:Key="boxSize"
                      x:TypeArguments="x:Double"
                      iOS="50"
                      Android="50"
                      WinPhone="75" />
    
          
          <OnPlatform x:Key="rowHeight"
                      x:TypeArguments="x:Int32"
                      iOS="60"
                      Android="60"
                      WinPhone="85" />
    
          <local:DoubleToIntConverter x:Key="intConverter" />
    
        </ResourceDictionary>
      </ContentPage.Resources>
    

    Get the right namespace and make an element like

    Then

    <Label Text="{Binding Color.G,
                                       Converter={StaticResource intConverter},
                                       ConverterParameter=255,
                                       StringFormat=', G={0:X2}'}" />
    

    oh yes, I watched this, but was not useful for me. then i was not able to find the converter i made from the xaml, when other converter was finded... so, i don't know how to proceed

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    You might need to post your issue more clearly then, as we don't really understand what the issues you are having are.

  • osharko-kunosharko-kun ITMember ✭✭

    I can't post all the code because of factory privacy.
    <my:SingolaRigaCollapsed Even="False" Height="40" HeaderText="Variazione patrimoniale &#x0a; al lordo dei proventi" ValueText="{Binding RendimentoAlLordoProventi}" ValueColor="{Binding RendimentoAlLordoProventi, Converter={StaticResource ValueToColorConverter}}"/>

    This is the line where there's the write, the ValueColor makes the crash.

    I also used the using directive for the converter:
    xmlns:conv="using:PROJ.WP.Converters"

    For the c# code... That's really so much to copy and paste, and i really don't know what paste because is a lot of code and i haven't founded the point where the xaml bind the text

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Do you have the ValueToColorConverter in your resources?

    You could post the converter and its namespace if that would help.

    Otherwise, the easiest way is to replicate it in a new test project and share that so we can actually help you, and Clint could tell you if a trigger would work better.

  • osharko-kunosharko-kun ITMember ✭✭

    this is the class of the converter:
    `class ValueToColorConverters : Windows.UI.Xaml.Data.IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, string language)
    {
    return value.ToString().Contains("-") ? Color.Red : Color.Green;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
    

    }`

    The namespace is that:

    namespace PROJ.WP.Converters;

    What i have to replicate? xaml? cs? cs as a lot of big parts, and the xaml has a lot of custom rendering... so even a single page of xaml is in real a long project

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    edited August 2017

    And so in your page you have a line like this?

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:conv="using:PROJ.WP.Converters"
                 x:Class="XamlSamples.ListViewDemoPage"
                 Title="ListView Demo Page">
    
      <ContentPage.Resources>
        <ResourceDictionary>
    
          <conv:ValueToColorConverters x:Key="ValueToColorConverter" />
    
        </ResourceDictionary>
      </ContentPage.Resources>
      <my:SingolaRigaCollapsed Even="False" Height="40"  HeaderText="Variazione patrimoniale &#x0a; al lordo dei proventi" ValueText="{Binding RendimentoAlLordoProventi}" ValueColor="{Binding RendimentoAlLordoProventi, Converter={StaticResource ValueToColorConverter}}"/> 
    </ContentPage>
    

    Just make it simple. Make a page, declare your converter like I did, bind it to a simple control (Label) instead of your fancy control there, and see if the converter gets hit.

Sign In or Register to comment.