選択したタブをユーザー設定: 選択済みにバインドしようとしていますが、「リソース設定を解決できませんでした」というエラーが表示されます。
問題は次の行にあります。
SelectedItem="{Binding Source={StaticResource Settings}, Path=Default.Selected, Converter={StaticResource SelectedTabConverter}}"
私の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>
私の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>
私のコンバータークラス:
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;
}
}
}
コンバーターは、設定とは異なり、App.xaml 以外の view.xaml に「登録」する必要があると思いますが、正しいですか? それとも違いはありませんか?
編集:移動しました
<converter:SelectedTabConverter x:Key="SelectedTabConverter"/>
App.xaml に、問題はまだ存在します...