Mik*_*scu 6 wpf binding styles
我有一个主菜单,mnuMainMenu包括几个子菜单.其中一个子菜单mnuMostRecentDirs本身是另一个菜单,它使用绑定到ObservableCollection的ItemSource属性在运行时生成它的项目.基本上它显示最近使用的文件夹列表.
问题是添加的MenuItems动态生成以下数据绑定错误:
主菜单的XAML声明如下:
<!-- Main Menu -->
<Menu x:Name="mnuMainMenu" Height="Auto" IsMainMenu="True">
<!--- ... more menu declarations before ... -->
<MenuItem x:Name="mnuitemWorkDirs" Header="Directories">
<MenuItem x:Name="mnuNewDir"
Header="New"
Style="{StaticResource MenuItem}"
Command="{x:Static cmds:Commands.NewDirectory}" />
<MenuItem x:Name="mnuCurrentDir"
Header="Current"
Style="{StaticResource MenuItem}"
Command="{x:Static cmds:Commands.CurrentDirectory}" />
<MenuItem x:Name="mnuMostRecentDirs"
Header="Recent Directories.."
Style="{StaticResource MenuItem}"
ItemsSource="{Binding ElementName=theMain, Path=MostRecentFoldersList}" />
</MenuItem>
<!--- ... more menu declarations after ... -->
</Menu>
Run Code Online (Sandbox Code Playgroud)
以下代码在运行时将文件夹添加到observable集合:
private void PopulateMostRecentFoldersList(string[] paths)
{
MostRecentFoldersList.Clear();
if (paths == null)
return;
foreach (var fullPath in paths)
{
var mi = new MenuItem();
mi.Command = Commands.ChooseWorkingDirectory;
mi.CommandParameter = fullPath;
string name = System.IO.Path.GetFileName(fullPath);
mi.Header = name.ToUpper();
// removing this style completely
// or manually setting the HorizontalContentAlignment property here
// prevents the binding error from happening
mi.Style = (Style)FindResource("MenuItem");
MostRecentFoldersList.Add(mi);
}
}
Run Code Online (Sandbox Code Playgroud)
我能够将问题跟踪到在MenuItem的样式中声明的绑定,该绑定未在我的应用程序中声明.我认为它是菜单项的默认样式..
没有其他菜单项似乎具有任何绑定问题,同一样式应用于所有.
所以我认为问题必须是我动态添加MenuItems的方法.更具体地说,似乎在将项添加到ItemsControl之前将样式应用于MenuItem.
到目前为止,我只能在将菜单添加到可观察集合之前在MenuItem上设置HorizontalContentAlignment属性,但这只是一个创可贴,它只是真正掩盖了真正的问题.
正如@LPL所指出的 - 事实证明这是WPF框架中已知(已确认)的问题.基于MSDN支持论坛此处和此处的信息,似乎在将MenuItem的ItemsSource设置为MenuItems的集合时,它不会以正确的顺序处理事物
每个MSDN支持人员:
将MenuItem.ItemsSource设置为包含MenuItems的Collection时会发生这种情况.此错误已在内部处理,因此您可以不管它.
或者您可以显式设置MenuItem.HorizontalContentAlignment属性和VerticalContentAlignment属性以避免此错误.您只需将以下代码放在application.Resource中即可实现此目的.
Run Code Online (Sandbox Code Playgroud)<Style TargetType="MenuItem"> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="VerticalContentAlignment" Value="Center"/> </Style>
我希望这有助于其他人遇到同样的问题.
我真的认为从代码添加 UIelemnts 不是一个好主意或“WPF 兼容”。
我会建议这样的事情:
<Menu>
<MenuItem Header="MainMenu" Name="sampleMenu">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Path=PathtoNameOfRecentDocObject}"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</Menu>
Run Code Online (Sandbox Code Playgroud)
并将 MainMenu 的 ItemsSource 设置为MostRecentFoldersList
| 归档时间: |
|
| 查看次数: |
1667 次 |
| 最近记录: |