C# 无法解析设置?(尝试使用转换器将用户设置与UI绑定)
我正在尝试将所选选项卡与用户设置绑定:selected,但我遇到了“资源设置无法解决”错误 问题出在这一行:C# 无法解析设置?(尝试使用转换器将用户设置与UI绑定),c#,wpf,C#,Wpf,我正在尝试将所选选项卡与用户设置绑定:selected,但我遇到了“资源设置无法解决”错误 问题出在这一行: SelectedItem="{Binding Source={StaticResource Settings}, Path=Default.Selected, Converter={StaticResource SelectedTabConverter}}" 我的xaml: <Window x:Class="MyHomework__MVVM_.MyHomeworkView"
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>
我认为转换器应该在view.xaml而不是App.xaml中“注册”,与设置不同,对吗?还是没有区别
编辑:
我搬家了
<converter:SelectedTabConverter x:Key="SelectedTabConverter"/>
对于App.xaml,问题仍然存在…试试这个
应用程序中的XAML
添加XAML名称空间,如下所示
xmlns:properties="clr-namespace:MyHomework__MVVM_.Properties"
<Application.Resources>
<properties:Settings x:Key="Settings" />
<Application.Resources>
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, ..."
绑定源对象是由应用程序的设置
类中的静态默认
属性返回的设置
实例。它不是在应用程序.Resources
中作为资源创建的实例。除非对该资源有任何其他引用,否则您可以直接删除它。试试这个
应用程序中的XAML
添加XAML名称空间,如下所示
xmlns:properties="clr-namespace:MyHomework__MVVM_.Properties"
<Application.Resources>
<properties:Settings x:Key="Settings" />
<Application.Resources>
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, ..."
绑定源对象是由应用程序的
设置
类中的静态默认
属性返回的设置
实例。它不是在应用程序.Resources
中作为资源创建的实例。除非对该资源有任何其他引用,否则您可以直接将其删除。否……设置不是类。这是我在Properties.SettingsIt中定义的用户设置,它抱怨“错误MC3022:添加到IDictionary的所有对象都必须具有键属性或与之相关联的其他类型的键。第8行位置10。“尝试设置x:Key=”设置“在@Somnath中,Application.Resources
中的新Settings
实例完全没有意义。你甚至不用它。@Clemens是的。但是,如果他在代码中定义了X=3,那么这句话就不会让我太烦恼了。这只是他的代码的一部分。不……设置不是一门课。这是我在Properties.SettingsIt中定义的用户设置,它抱怨“错误MC3022:添加到IDictionary的所有对象都必须具有键属性或与之相关联的其他类型的键。第8行位置10。“尝试设置x:Key=”设置“在@Somnath中,Application.Resources
中的新Settings
实例完全没有意义。你甚至不用它。@Clemens是的。但是,如果他在代码中定义了X=3,那么这句话就不会让我太烦恼了。这只是他的密码的一部分。