2011-10-06 70 views
0

我有我的用户声明一个控件属性为静态资源

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> 

Static.StaticInfoColl不XML命名空间中存在的标签clr-namespace:AAA.Presentation

有人能帮助我什么,我做错了什么?


的用户控件的名称是静态[X:姓名]

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

我有对他们的多个按钮的用户控件。 按钮具有一个StackPanel内,如下所示:

<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

发表您的整个'Static'类。 –

+1

这没有多大意义。你不能创建一个属性的实例,你可以创建一个具有这个(和更多)属性的对象实例。 “UserControl.Resources”部分用于控件中使用的其他类或对象,如画笔,动画,样式,模板等。还有什么是“静态”。在你的xaml中?一个命名空间?这就是别名“local:”的意思。请提供更多详细信息,你想要做什么。 – dowhilefor

回答

3

如果我理解正确:

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

如果是这样的话......你靠近......但我会做,而不是执行以下操作:

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

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

然后,当你要访问您的StaticInfoCollection对象在你的用户控件的代码:

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

嗨斯科特,感谢您的快速响应 - 本地被定义为xmlns:local =“clr-namespace:AAA.Presentation”,但StaticInfoCollection是ucBrand [AAA.Presentation.ucBrand]下的属性,我如何创建一个资源指向该属性。 – Raj

+0

'StaticInfoCollection'似乎是一种类型。 'StaticInfoColl'是你的那种类型的属性。我的建议是删除该属性,而是创建一个StaticInfoCollection(类型)的实例作为资源。然后在代码中...而不是访问你的属性'StaticInfoColl',你可以使用'this来访问它。FindResource(...)”。这完全取决于你想要在XAML中使用这个类的实例。 – Scott

+0

你能解释一下你希望用XAML中的属性来做什么吗? (例如,将文本的值显示为文本我认为你想做的事情越多......我认为你应该简单地绑定到你的财产......而不是实际上将其创建为资源。 – Scott