Álvaro García
Álvaro García

Reputation: 19396

How to set a property of a custom control from the main window?

I have a custom control, this is the generic.axml code:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Calendario"
    xmlns:MyNamespace="clr-namespace:Calendario;assembly=Calendario"
    xmlns:Converters="clr-namespace:Calendario.Converters">

    <Converters:DateConverter x:Key="DateConverter"></Converters:DateConverter>
    <Converters:DayBorderColorConverter x:Key="DayBorderColorConverter"></Converters:DayBorderColorConverter>

    <Style TargetType="{x:Type local:CalendarioPersonalizado}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CalendarioPersonalizado}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <DockPanel>
                            <TextBlock Text="{Binding Date}" />

                            <Grid Height="30" DockPanel.Dock="Top">

                            </Grid>


                            <ItemsControl ItemsSource="{Binding Days}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border BorderBrush="{Binding ColorRecuadroExterno, Mode=TwoWay}" BorderThickness="1" Padding="0">                                         
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

And I have my Calendario.cs with the dpendency property:

public static readonly DependencyProperty ColorRecuadroExternoProperty = DependencyProperty.Register("ColorRecuadroExterno", typeof(Brush), typeof(CalendarioPersonalizado));
public Brush ColorRecuadroExterno
{
    get { return (Brush)GetValue(ColorRecuadroExternoProperty); }
    set { SetValue(ColorRecuadroExternoProperty, value); }
}

And later in my main windows I use the control:

<local:CalendarioPersonalizado x:Name="cCalendario" ColorRecuadroExterno="Green"/>

The problem is that the border of the day in the calendar is not set to green like I have tried to set in the main window.

Also in the code behid I have tried this:

cCalendario.ColorRecuadroExterno = System.Windows.Media.Brushes.Green;

But the the color is not set.

What I want to do is set the color of the border in my custom cotrol from my main application.

Thanks.

Upvotes: 0

Views: 60

Answers (2)

FreeMan
FreeMan

Reputation: 1457

If you put a Callback method in your local:CalendarioPersonalizado class and set your backround in this callback method. I think it is going to work.

public static readonly DependencyProperty ColorRecuadroExternoProperty = DependencyProperty.Register("ColorRecuadroExterno", typeof(Brush), typeof(CalendarioPersonalizado),
    new PropertyMetadata(Brushes.Brown, Callback));

private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    CalendarioPersonalizado obj = d as CalendarioPersonalizado;
    obj.ColorRecuadroExterno.Background = (Brush)e.NewValue;
}

Upvotes: 1

s_mg_
s_mg_

Reputation: 418

Your DependencyProperty is of type Brush. You can't implicitly convert "Green" to a Brush.

You either need to use a converter to convert a string representation of a color to a brush, or make your property type Color, and bind it to an appropriate property.

Upvotes: 0

Related Questions