Trouble displaying an object in WPF

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-03-23T18:39:04Z Indexed on 2010/03/23 18:43 UTC
Read the original article Hit count: 341

Filed under:
|
|

I'm so new to this that I can't even phrase the question right...

Anyway, I'm trying to do something very simple and have been unable to figure it out. I have the following class:

public class Day : Control, INotifyPropertyChanged
{
    public static readonly DependencyProperty DateProperty =
        DependencyProperty.Register("Date", typeof(int), typeof(Day));

    public int Date
    {
        get { return (int)GetValue(DateProperty); }
        set 
        { 
            SetValue(DateProperty, value); 
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Date"));
            }
        }
    }

    public static readonly DependencyProperty DayNameProperty =
        DependencyProperty.Register("DayName", typeof(String), typeof(Day));

    public String DayName
    {
        get { return (String)GetValue(DayNameProperty); }
        set 
        { 
            SetValue(DayNameProperty, value); 
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("DayName"));
            }
        }
    }

    static Day()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Day), new FrameworkPropertyMetadata(typeof(Day)));
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

I've learned that you can't call a constructor that has parameters in XAML so the only way to actually set some data for this class is through the two properties, DayName and Date.

I created a ControlTemplate for Day which is as follows:

<Style TargetType="{x:Type con:Day}">
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="MinWidth" Value="80"/>
    <Setter Property="Height" Value="20"/>
    <Setter Property="Width" Value="80"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type con:Day}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Rectangle Grid.ColumnSpan="2" x:Name="rectHasEntry" Fill="WhiteSmoke"/>    
                    <TextBlock Grid.Column="0" x:Name="textBlockDayName" Text="{TemplateBinding DayName}" FontFamily="Junction" FontSize="11" Background="Transparent"
                               HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,2,0,0"/>
                    <TextBlock Grid.Column="1" x:Name="textBlockDate" Text="{TemplateBinding Date}" FontFamily="Junction" FontSize="11" Background="Transparent"
                               HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,2,0,0"/>
                    <Rectangle Grid.ColumnSpan="2" x:Name="rectMouseOver" Fill="#A2C0DA" Opacity="0"
                               Style="{StaticResource DayRectangleMouseOverStyle}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I then render it on screen in my MainWindow thusly:

<Window x:Class="WPFControlLibrary.TestHarness.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:con="clr-namespace:WPFControlLibrary.Calendar;assembly=WPFControlLibrary"
    Title="MainWindow" Height="500" Width="525"
    WindowStartupLocation="CenterScreen">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="80"/>
    </Grid.ColumnDefinitions>
    <con:Day Grid.Column="1" Height="20" Width="80" DayName="Mon" Date="1"/>
</Grid>

And what I actually see is, well, nothing. If I put my cursor on the con:Day line of the XAML it'll highlight the correctly sized rectangle in the window but I don't see "Mon" on the left side of the rectangle and "1" on the right.

What am I doing wrong? I suspect it's something simple but I'll be darned if I'm seeing it.

My ultimate goal is to group a bunch of the Day controls within a Month control, which is then contained in a Year control as I'm trying to make a long Calendar Bar that lets you navigate through the months and years, while clicking on a Day would display any information saved on that date. But I can't even get the Day part to display independent of anything else so I'm a long way from the rest of the functionality. Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml