2011-11-16 11 views
9

this exampleを実行しようとしましたが、バインディングの問題があります。WPFバインディング:静的リソースを解決できません

デザイナーは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(); 
    } 
} 
+0

外部のウェブサイトやブログにリンクするのではなく、コードの関連部分を投稿してください。 –

答えて

12

あなたはこのビットを逃している:

- >このユーティリティクラスは、リソース<としてインスタンス化することができます - ComboBox作成で参照されている です。

それはこのようなものを見てみましょう:このビット

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

app.xamlで以下のようなエントリを追加してください。 :

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Skins/ControlSkin.xaml"> 
     </ResourceDictionary> 
      <ResourceDictionary Source="/Skins/ListBox.xaml"> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDicionaries> 
    </ResourceDictionary> 
</Application.Resources> 
関連する問題