将usercontrol属性声明为静态资源

时间:2011-10-06 10:52:23

标签: wpf

我在usercontrol中定义了一个属性

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

我希望能够在xaml中使用它。 但是每当我按照以下方式声明属性时

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

XML名称空间Static.StaticInfoColl

中不存在标记clr-namespace:AAA.Presentation

有人可以帮我解决我的错误吗?


usercontrol的名称是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">

我有一个usercontrol,上面有多个按钮。 按钮的堆栈面如下:

<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>

我有一个实体InfoInfoCollection 该按钮绑定到InfoCollection,基于FileType我想选择适当的FileDownloadDate并显示它。

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));
        }

    }
}

1 个答案:

答案 0 :(得分:3)

如果我理解正确的话:

  • 您有一个UserControl(ucBrand)。
  • 您有一个类StaticInfoCollection
  • 您希望在ucBrand中的多个位置使用一个StaticInfoCollection实例,并且您可能也希望在代码中访问您的StaticInfoCollection对象。

如果是这样的话......你很接近......但我会做以下事情:

在XAML中定义StaticInfoCollection对象的实例。

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

然后当你想在UserControl的代码中访问你的StaticInfoCollection对象时:

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