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