Nime Cloud
Nime Cloud

Reputation: 6387

WPF Binding: Static resource cannot be resolved

I tried to run this example but I got binding problem.

Designer highlights the error The resource "monthCollection" could not be resolved

How can I use Utility.MonthCollection as local resource?

XAML part:

<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# part:

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

Upvotes: 12

Views: 35600

Answers (2)

Smitha Veerappa
Smitha Veerappa

Reputation: 11

Please add an entry like below in app.xaml:

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

Upvotes: 1

N_A
N_A

Reputation: 19897

You're missing this bit:

->this Utility class can be instantiated as a resource<- and then referenced in the ComboBox creation.

It'll look something like this:

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

This bit:{Binding Source={StaticResource monthCollection}, Path=MonthCollection says to find the static resource monthCollection and on it use the property MonthCollection so you first must instantiate the object which has `MonthCollection as a property and then reference that static resource.

You will probably also need a statement something like this added to the top of your file:

xmlns:local="clr-namespace:YourNamespaceHere"

Untested code below:

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

Upvotes: 18

Related Questions