Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将命令与MenuItem';WPF中的s_C#_Wpf_Mvvm - Fatal编程技术网

C# 将命令与MenuItem';WPF中的s

C# 将命令与MenuItem';WPF中的s,c#,wpf,mvvm,C#,Wpf,Mvvm,因此,我有一个MenuItem,它使用CultureInfos列表作为项目源。 我试图做的是在单击/选择CultureInfo时启动一个函数,该函数用于将应用程序的本地化更改为所选CultureInfo。 我可以有把握地说,该函数正在正常工作。 在研究和尝试了几个示例后,我得出了以下结论,但不幸的是,这不起作用: <MenuItem Header="{lex:LocText MenuLanguages}" ItemsSource="{Binding LanguageList}" Displ

因此,我有一个
MenuItem
,它使用
CultureInfo
s列表作为项目源。
我试图做的是在单击/选择
CultureInfo
时启动一个函数,该函数用于将应用程序的本地化更改为所选CultureInfo。 我可以有把握地说,该函数正在正常工作。 在研究和尝试了几个示例后,我得出了以下结论,但不幸的是,这不起作用:

<MenuItem Header="{lex:LocText MenuLanguages}" ItemsSource="{Binding LanguageList}" DisplayMemberPath="Name">
        <MenuItem.ItemContainerStyle>
            <Style>
                <Setter Property="MenuItem.Command" Value="{Binding SetLanguage}" />
                <Setter Property="MenuItem.CommandParameter" Value="{Binding}" />
            </Style>
        </MenuItem.ItemContainerStyle>
</MenuItem>
CultureInfo.GetCultureInfo()
将字符串作为参数。
itemsource基本上是一个
可观察的集合

我的问题是上面的代码有什么问题?我已经尝试了很多“解决方案”,但都没有真正起作用…
当我从列表中选择一个项目时,什么也没有发生

*我还尝试在
ChangeLanguage
方法上设置一个断点,这让我得出结论,该方法甚至从未触发过


使用
caliburn.micro
WPFLocalizationExtension
extension

如果不起作用,那么绑定是错误的。在调试窗口中检查绑定错误。 您需要使用relativesource,因为直接绑定不会做您认为会做的事情

    <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
    <MenuItem.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
            <Setter Property="CommandParameter" Value="{Binding}" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

如果不起作用,则绑定错误。在调试窗口中检查绑定错误。 您需要使用relativesource,因为直接绑定不会做您认为会做的事情

    <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
    <MenuItem.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
            <Setter Property="CommandParameter" Value="{Binding}" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

A
菜单
的逻辑树与其余的
标记
不同。因此,您需要按照Erti Chris的建议,首先搜索正确的
DataContext


此外,每个DataTemplate都有类型T的DataContext,它是绑定项资源列表的一部分。

A
菜单
的逻辑树与
标记的其余部分不同。因此,您需要按照Erti Chris的建议,首先搜索正确的
DataContext


此外,每个DataTemplate都有类型T的DataContext,它是绑定项资源列表的一部分。

woah谢谢!它起作用了!你能进一步解释一下为什么“正常绑定”不起作用吗?有没有其他控件的行为相同?嘿,看看我的评论。我解释了为什么这个装订在你的箱子里不起作用。哇,谢谢!它起作用了!你能进一步解释一下为什么“正常绑定”不起作用吗?有没有其他控件的行为相同?嘿,看看我的评论。我解释了为什么在你的情况下这种装订不起作用。