1

我正在尝试将所选选项卡与用户设置绑定:已选择,但我收到此“资源设置无法解析”错误。

问题出在这一行:

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

我的xml:

<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,问题仍然存在......

4

1 回答 1

1

试试这个

在 App.XAML 中

如下添加 XAML 命名空间

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

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

在您的页面中

如下添加 XAML 命名空间

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

现在设置绑定如下

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

编辑:请注意,如上所示绑定时,

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

绑定源对象是应用程序类中Settings的静态属性返回的实例。它不是作为资源创建的实例。除非对该资源有任何其他引用,否则您可以简单地删除它。DefaultSettingsApplication.Resources

于 2013-01-21T06:32:47.610 回答