【问题标题】:WPF Binding: Static resource cannot be resolvedWPF 绑定:无法解析静态资源
【发布时间】:2011-12-30 12:52:28
【问题描述】:

我尝试运行this example,但遇到绑定问题。

设计器突出显示错误The resource "monthCollection" could not be resolved

如何将 Utility.MonthCollection 用作本地资源?

XAML 部分:

<Window x:Class="FaceReport.WindowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Rapor" Height="402" Width="600" WindowState="Normal">
<Grid Name="gridMain" x:Uid="uidGridMain">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox SelectedIndex="0" 
              DisplayMemberPath="Value" SelectedValuePath="Key" Margin="132,9,200,0"
              Grid.Row="3" Height="24" VerticalAlignment="Top" Name="cbBind" 

              ItemsSource="{Binding Source={StaticResource Utility.ReportForCollection},
                Path=Utility.ReportForCollection}"                   
              />
 </Grid>
 </Window>

C#部分:

namespace FaceReport
{
internal class Utility
{
    public enum ReportFor
    {
        Choose,
        All,
        Group,
        Person
    }

    private static Dictionary<ReportFor, string> _dictReportFor;
    public static Dictionary<ReportFor, string> ReportForCollection
    {
        get
        {
            return _dictReportFor;
        }
    }

    static Utility()
    {
        //initialize the collection with user friendly strings for each enum
        _dictReportFor = new Dictionary<ReportFor, string>(){
            {ReportFor.Choose, "Lütfen seçiniz..."},        
            {ReportFor.All, "Herkes"},
            {ReportFor.Group, "Grup"},
            {ReportFor.Person, "Şahıs"}};
    }
}

/// <summary>
/// Application's main form
/// </summary>
public partial class WindowMain : Window
{
    /// <summary>
    /// Constructor
    /// </summary>
    public WindowMain()
    {
        InitializeComponent();
    }
}

【问题讨论】:

  • 请发布您的代码的相关部分,而不是链接到外部网站或博客。

标签: c# wpf xaml binding


【解决方案1】:

你错过了这一点:

->这个 Utility 类可以实例化为资源

它看起来像这样:

<Application.Resources>
    <local:Utility x:Key="monthCollection"/>
</Application.Resources>

这个位:{Binding Source={StaticResource monthCollection}, Path=MonthCollection 说要找到静态资源monthCollection 并在其上使用属性MonthCollection,因此您首先必须实例化具有`MonthCollection 作为属性的对象,然后引用该静态资源。

您可能还需要在文件顶部添加类似这样的语句:

xmlns:local="clr-namespace:YourNamespaceHere"

以下未经测试的代码:

<Window x:Class="FaceReport.WindowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FaceReport"

 Title="Rapor" Height="402" Width="600" WindowState="Normal">

<Application.Resources>
    <local:Utility x:Key="reportCollection"/>
</Application.Resources>

 <Grid Name="gridMain" x:Uid="uidGridMain">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox SelectedIndex="0" DisplayMemberPath="Value" SelectedValuePath="Key"  Margin="132,9,200,0" Grid.Row="3" Height="24" VerticalAlignment="Top" Name="cbBind" 
     ItemsSource="{Binding Source={StaticResource reportCollection}, Path=ReportForCollection}" />
 </Grid>
</Window>

【讨论】:

    【解决方案2】:

    请在 app.xaml 中添加如下条目:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
              <ResourceDictionary Source="/Skins/ControlSkin.xaml">
             </ResourceDictionary>
                <ResourceDictionary Source="/Skins/ListBox.xaml">
              </ResourceDictionary>
            </ResourceDictionary.MergedDicionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    【讨论】:

      猜你喜欢
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      相关资源
      最近更新 更多