Raj
Raj

Reputation: 77

Declaring a usercontrol property as a static resource

I have a property defined within my usercontrol

private StaticInfoCollection _StaticInfoColl;
public StaticInfoCollection StaticInfoColl
{
    get { return _StaticInfoColl; }
    set
    {
        if (value == _StaticInfoColl) return;
        _StaticInfoColl = value;
    }
}

I would like to be able to use this in the xaml. However whenever I do declare the property as follows

<UserControl.Resources>
    <local:Static.StaticInfoColl x:Key="SIColl" />
</UserControl.Resources>

The tag Static.StaticInfoColl does not exist in XML namespace clr-namespace:AAA.Presentation

Could someone help me on what I am doing wrong?


The name of the usercontrol is Static [x:Name]

<UserControl x:Class="AAA.Presentation.ucBrand"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local ="clr-namespace:MBCL.Presentation"
         mc:Ignorable="d" 
         d:DesignHeight="710" 
         d:DesignWidth="1025" 
         Height="710" 
         Width="1025" 
         x:Name="Static" 
         HorizontalAlignment="Left" 
         HorizontalContentAlignment="Left">

I have a usercontrol with multiple buttons on them. The buttons have a stackpanel within it as follows :

<Button Style="{StaticResource appViewButton}" DataContext="{Binding}" >
    <StackPanel>
        <TextBlock FontSize="11">View Products</TextBlock>
        <TextBlock  Text="{Binding Path=FileDownloadDate,StringFormat='Last Uploaded : {0:dd-MMM-yyyy}'}"
                Style="{StaticResource tbUploadDate}" 
                HorizontalAlignment="Center" />
    </StackPanel>
</Button>

I have an entity Info and InfoCollection.
The button is bound to InfoCollection, based on the FileType I would like to select the appropriate FileDownloadDate and display it.

public class Info
{

    public Info(DataRow dr)
    {
        _FileType         = Util.HandleNull<string>(dr[AppConstants.FILE_TYPE]);
        _FileID           = Util.HandleNull<long?>(dr[AppConstants.FILEID]);
        _FileTypeDesc     = Util.HandleNull<string>(dr[AppConstants.CODE_DESC]);
        _FileDownloadDate = Util.HandleNull<DateTime>(dr[AppConstants.DOWNLOAD_DATE]);
    }

    private string _FileType;
    public string FileType
    {
        get { return _FileType; }
        set
        {
            if (value != _FileType)
                _FileType = value;
        }
    }

    private string _FileTypeDesc;
    public string FileTypeDesc
    {
        get { return _FileTypeDesc; }
        set
        {
            if (value != _FileTypeDesc)
                _FileTypeDesc = value;
        }
    }

    private long?   _FileID;
    public long?    FileID
    {
        get { return _FileID; }
        set
        {
            if (value != _FileID)
                _FileID = value;
        }
    }

    private DateTime    _FileDownloadDate;
    public  DateTime    FileDownloadDate
    {
        get { return _FileDownloadDate; }
        set
        {
            if (value != _FileDownloadDate)
                _FileDownloadDate = value;
        }
    }

}

public class InfoCollection : ObservableCollection<Info>
{
    public InfoCollection(DataTable dtStaticInfo)
    {
        foreach (DataRow drSInfo in dtStaticInfo.Rows)
        {   
            this.Add(new StaticInfo(drSInfo));
        }

    }
}

Upvotes: 0

Views: 4687

Answers (1)

Scott
Scott

Reputation: 12050

If I understand you correctly:

  • You have a UserControl (ucBrand).
  • You have a class StaticInfoCollection
  • You want to have a single instance of StaticInfoCollection used in multiple places within ucBrand, and you might want to access your StaticInfoCollection object in code as well.

If that's the case... you're close... but I'd do the following instead:

Define the instance of your StaticInfoCollection object in XAML.

<UserControl.Resources> 
    <local:StaticInfoCollection x:Key="SIColl" /> 
</UserControl.Resources> 

Then when you want to access your StaticInfoCollection object in your UserControl's code:

StaticInfoCollection staticInfoColl = (StaticInfoCollection)this.FindResource("SIColl");

Upvotes: 3

Related Questions