Yaroslav Shabanov
Yaroslav Shabanov

Reputation: 283

color in xaml from c#(windows phone)

XAML:

<Rectangle Fill="{Binding Gray}" />

C#:

public class colors
{
    public string Gray
    {
        set {}
        get{ return "#FF22262a";}
    }
}

There is no error when I compile. But rectangle does not fill in "#FF22262a"

EDIT: This code does not work too:
MainPage.xaml

<TextBlock Text="{Binding MyGray2}"></TextBlock>

MainPage.xaml.cs

public String MyGray2
{
    set { }
    get { return "gjnuegheugheog"; }
}

EDIT2:

<phone:PhoneApplicationPage 
    x:Class="FlagLib.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:FlagLib"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
            <vm:colors x:Key="vmColors"  />
...
    </phone:PhoneApplicationPage.Resources>
...
            <Grid Grid.Column="0" Tap="onToggleHorizontal" DataContext="{StaticResource vmColors}">
                <Rectangle Fill="{Binding Gray}" />
...

EDIT3:
MainPage.xaml:

<phone:PhoneApplicationPage
    x:Class="proba5.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:vm="clr-namespace:proba5" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <vm:colors x:Key="vmColors"/>
    </phone:PhoneApplicationPage.Resources>


    <Grid x:Name="LayoutRoot" Background="Transparent">


        <Grid DataContext="{StaticResource vmColors}">
            <Rectangle Fill="{Binding Gray}" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

colors.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proba5
{
    public class colors
    {
        public string Gray
        {
            set { }
            get { return "#FF22262a"; }
        }
    }
}

Why i got The name "colors" does not exist in the namespace "clr-namespace:proba5".

Upvotes: 0

Views: 1854

Answers (2)

keyboardP
keyboardP

Reputation: 69372

You need to set your DataContext.

public MyPage()
{
   DataContext = new colors();
}

I prefer to do it in XAML.

Import your namespace at the top

xmlns:vm="clr-namespace:MyNamespace"

Add the class as a resource (you can do this at app level if you prefer)

<phone:PhoneApplicationPage.Resources>
    <vm:colors x:Key="vmColors"  />
</phone:PhoneApplicationPage.Resources>

Assign to the DataContext (in this case I've assigned it to the Grid so any control within the Grid will assume the vmColors DataContext, unless you change it for that particular child control).

<Grid DataContext="{StaticResource vmColors}">
   <Rectangle Fill="{Binding Gray}" />
</Grid>

Here's the full XAML for a page so you can see where the code goes.

<phone:PhoneApplicationPage
    x:Class="MyClass.MyPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:vm="clr-namespace:MyNamespace" // << IMPORT NAMESPACE
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d">


    //ALLOW THE CLASS TO BE ACCESSED VIA STATICRESOURCE
    <phone:PhoneApplicationPage.Resources>
        <vm:colors x:Key="vmColors"/> 
    </phone:PhoneApplicationPage.Resources>


    <Grid x:Name="LayoutRoot" Background="Transparent">      

        //SET THE DATACONTEXT OF THE GRID TO THE COLORS CLASS
        <Grid DataContext="{StaticResource vmColors}">
            <Rectangle Fill="{Binding Gray}" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

Upvotes: 2

Atlasmaybe
Atlasmaybe

Reputation: 1549

You can't do that. What you're trying to is implicitly cast a string into a Color, which is not possible. You should better considering this code :

public SolidColorBrush MyGray
{
    set { }
    get { return new SolidColorBrush(Color.FromArgb(172, 172, 172, 0)); }
}

Here is my source : http://msdn.microsoft.com/fr-fr/library/system.windows.shapes.shape.fill.aspx

There are other ways to manage color in XAML, you could browse msdn for more information.

Edit : It's normal that you have no error on compilation. XAML is interpreted at execution.

Edit 2 : Update the code.

Upvotes: 0

Related Questions