1

I'm trying to bind the selected tab with an user setting: Selected, but I'm getting this "The resource Settings could not be resolved" error.

The problem is at this line:

SelectedItem="{Binding Source={StaticResource Settings}, Path=Default.Selected, Converter={StaticResource SelectedTabConverter}}"

My xaml:

<Window x:Class="MyHomework__MVVM_.MyHomeworkView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converter="clr-namespace:MyHomework__MVVM_"
       Title="My Homework" Height="450" Width="800" ResizeMode="CanMinimize">
    <Window.Resources>
        <converter:SelectedTabConverter x:Key="SelectedTabConverter"/>
    </Window.Resources>
    <Grid Margin="0,0,10,10">
        <TabControl HorizontalAlignment="Left" Height="330" VerticalAlignment="Top" Width="764" Margin="10,10,0,0" ItemsSource="{Binding AllTabs}" SelectedItem="{Binding Source={StaticResource Settings}, Path=Default.Selected, Converter={StaticResource SelectedTabConverter}}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Header}"/>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="16" AcceptsReturn="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextChanged="OnTextChanged">
                                    </TextBox>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="FontSize" Value="20"/>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
        <Button Content="Add Course" HorizontalAlignment="Left" VerticalAlignment="Top" Width="105" Margin="10,351,0,0" Height="50" Command="{Binding AddCourseCommand}"/>
        <Button Content="Drop Course" HorizontalAlignment="Left" VerticalAlignment="Top" Width="76" Margin="126,379,0,0" Height="22" Command="{Binding DropCourseCommand, UpdateSourceTrigger=PropertyChanged}"/>
        <Button Content="Save HW" HorizontalAlignment="Left" VerticalAlignment="Top" Width="105" Margin="669,351,0,0" Height="50" Command="{Binding SaveHomeworkCommand, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

My App.xaml:

<Application x:Class="MyHomework__MVVM_.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:properties="clr-namespace:MyHomework__MVVM_.Properties"
             Exit="OnExit"
             StartupUri="MyHomeworkView.xaml">
    <Application.Resources>
        <properties:Settings x:Key="Selected"/>
    </Application.Resources>
</Application>

My converter class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace MyHomework__MVVM_
{
    class SelectedTabConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object result = DependencyProperty.UnsetValue;

            if (value != null)
            {
                int index = (int)value;
                result = MyHomeworkViewModel.GetTabs()[index];
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object result = DependencyProperty.UnsetValue;

            if (value != null)
            {
                MyHomeworkModel tab = (MyHomeworkModel)value;
                result = MyHomeworkViewModel.GetTabs().IndexOf(tab);
            }

            return result;
        }
    }
}

I believe the converter should be "registered" in view.xaml other than App.xaml, unlike Settings, correct? Or is there no difference?

Edit: I moved the

<converter:SelectedTabConverter x:Key="SelectedTabConverter"/>

to App.xaml, problem still exists...

user1447343
  • 1,417
  • 4
  • 18
  • 24

1 Answers1

1

Try this

In App.XAML

Add XAML namespace as below

xmlns:properties="clr-namespace:MyHomework__MVVM_.Properties"

<Application.Resources>
    <properties:Settings x:Key="Settings" />
<Application.Resources>

In your page

Add XAML namespace as below

xmlns:properties="clr-namespace:MyHomework__MVVM_.Properties"

Now set the Binding as Below

SelectedItem="{Binding Source={x:Static properties:Settings.Default}, Path=Selected, Converter={StaticResource SelectedTabConverter}}"

EDIT: Note that when binding like shown above,

SelectedItem="{Binding Source={x:Static properties:Settings.Default}, Path=Selected, ..."

the binding source object is a Settings instance returned by the static Default property in your application's Settings class. It is not the instance that was created as resource in Application.Resources. Unless there are any other references to that resource, you might simply drop it.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Somnath
  • 3,247
  • 4
  • 28
  • 42
  • No...Settings is not a class. It's a User Setting that I defined in Properties.Settings – user1447343 Jan 21 '13 at 06:46
  • It complains about "error MC3022: All objects added to an IDictionary must have a Key attribute or some other type of key associated with them. Line 8 Position 10." – user1447343 Jan 21 '13 at 07:12
  • Try setting x:Key="Settings" in – Somnath Jan 21 '13 at 07:28
  • @Somnath The new `Settings` instance in `Application.Resources` is completely pointless. You binding does not even use it. – Clemens Jan 21 '13 at 09:10
  • @Clemens That's right. But if he defines X=3 in his code that statement really does not bother me much. It’s just a part of his code. – Somnath Jan 21 '13 at 09:55
  • @Somnath You could have told him or her what he/she's doing wrong. – Clemens Jan 21 '13 at 10:00
  • @Clemens, I'm not here to teach all basics of WPF :). Instead of wasting time you should edit the answer if you like to add more. BTW there are lots of cases when we define Setting reference in Application.Resources. There is nothing wrong defining Settings in Application.Resources. He/she might be using it for some other purpose to make the xaml more compact, readable by avoiding big expression in binding and that's really is not the problem that he/she was facing! – Somnath Jan 21 '13 at 10:26