我不太确定这是否是问这个问题的最佳地点,但我想我应该尝试一下。
我正在尝试使用 MahApps Metro UI Toolkit 在我的应用程序中实现汉堡菜单,该工具包在我制作的测试窗口中效果很好。因此,我开始转向使用视图模型的主窗口之一。
问题是,HamburgerMenu 内的任何绑定根本不起作用,但在HamburgerMenu 控件之外却可以正常工作。
[示例视图:
这张图显示了我为展示示例而制作的测试窗口模型。标题和两个文本框都绑定到“我的标题”。正如您所看到的,汉堡菜单中的菜单不起作用。
我也尝试将 ViewModel 绑定到汉堡菜单控件,但这不起作用。我想它必须绑定到汉堡菜单控件模板中的“TheContentGrid”或“TheContent”控件,但这些控件无法从隐藏代码访问。
XAML:
<Controls:MetroWindow
..
Title="{Binding MyTitle}"
..>
<Grid>
<Grid.Resources>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type Controls:HamburgerMenuGlyphItem}">
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe MDL2 Assets" Text="{Binding Glyph}" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="16" Text="{Binding Label}" />
</Grid>
</DataTemplate>
</Grid.Resources>
<TextBox Text="{Binding MyTitle}" HorizontalAlignment="Right" />
<Controls:HamburgerMenu Margin="0 30 0 0"
SelectedIndex="0"
x:Name="HamburgerMenuControl"
ItemTemplate="{StaticResource MenuItemTemplate}"
OptionsItemTemplate="{StaticResource MenuItemTemplate}"
ItemClick="HamburgerMenuControl_OnItemClick"
OptionsItemClick="HamburgerMenuControl_OnItemClick"
DisplayMode="CompactOverlay">
<!-- Items -->
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="" Label="Home">
<Controls:HamburgerMenuGlyphItem.Tag>
<Grid>
<TextBox Text="{Binding MyTitle}" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<TextBlock Text="Home View" FontSize="32" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Controls:HamburgerMenuGlyphItem.Tag>
</Controls:HamburgerMenuGlyphItem>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.ItemsSource>
<!-- Content -->
<Controls:HamburgerMenu.ContentTemplate>
<DataTemplate DataType="{x:Type Controls:HamburgerMenuItem}">
<Grid x:Name="TheContentGrid">
<ContentControl x:Name="TheContent" Content="{Binding Tag}" />
</Grid>
</DataTemplate>
</Controls:HamburgerMenu.ContentTemplate>
</Controls:HamburgerMenu>
</Grid>
</Controls:MetroWindow>
代码隐藏和视图模型
public partial class WindowTest : MetroWindow
{
public WindowTest()
{
InitializeComponent();
DataContext = new TestViewModel();
}
private void HamburgerMenuControl_OnItemClick(object sender, ItemClickEventArgs e)
{
HamburgerMenuControl.Content = e.ClickedItem;
HamburgerMenuControl.IsPaneOpen = false;
}
}
public class TestViewModel : BindableBase
{
private string myTitle;
public string MyTitle
{
get { return myTitle; }
set { SetProperty(ref myTitle, value); }
}
public TestViewModel()
{
MyTitle = "Title from VM";
}
}
如果需要更多信息,请告诉我。
也许我的回答对你来说有点晚了,但可能对其他人有帮助。
我自己偶然发现了这个问题,并且可以使用本文中描述的解决方案解决它answer https://stackoverflow.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor.
Because HamburgerMenuGlyphItem
不会成为视觉或逻辑树的一部分,没有绑定祖先DataContext
。即您的绑定没有可绑定的源。
在资源中创建辅助对象的实例,并使用显式设置绑定源StaticResource
:
<Controls:MetroWindow.Resources>
<local:BindingProxy x:Key="Proxy" Data="{Binding}" />
</Controls:MetroWindow.Resources>
...
<Controls:HamburgerMenuGlyphItem.Tag>
<Grid>
<TextBox Text="{Binding Data.MyTitle, Source={StaticResource Proxy}}" />
</Grid>
</Controls:HamburgerMenuGlyphItem.Tag>
这是辅助对象的代码(source http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/):
public class BindingProxy : Freezable
{
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)