2011-11-16 85 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>