我正在尝试在我的应用程序中使用来自 MahApps.Metro 的 Flyouts。所以我将此部分添加到我的 MainWindow.xaml 中:
<controls:MetroWindow.Flyouts>
<controls:FlyoutsControl ItemsSource="{Binding Flyouts}">
<controls:FlyoutsControl.ItemTemplate>
<DataTemplate DataType="{x:Type viewModel:SettingsViewModel}">
<view:SettingsFlyout/>
</DataTemplate>
</controls:FlyoutsControl.ItemTemplate>
</controls:FlyoutsControl>
</controls:MetroWindow.Flyouts>
ItemTemplate
将包含从我的 View 模型到 View 的映射。 Flyouts
是 ObservableCollection<IFlyoutViewModel>
目前只包含我的SettingsViewModel
.IFlyoutViewModel
定义:using System.ComponentModel;
namespace MyApplication.ViewModel
{
internal interface IFlyoutViewModel : INotifyPropertyChanged
{
bool Visible { get; set; }
}
}
以及我如何使用
Visible
-属性(property):<controls:Flyout x:Class="MyApplication.View.SettingsFlyout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
Header="Settings"
Position="Right"
IsOpen="{Binding Visible}"
Width="300">
...
</controls:Flyout>
所以现在我设置
Visible
- 我的 SettingsViewModel 的属性,但 Flyout 不会打开。我究竟做错了什么?我只是尝试分配
IsOpen="true"
硬编码,但这也不起作用。因此,使用数据模板显示弹出似乎是问题所在......
最佳答案
我按照 Eldho 链接的问题讨论中的描述构建了它,现在它可以工作了。定义ItemContainerStyle
的关键并绑定(bind)IsOpen
那里!
新MainWindow.xaml
:
<controls:MetroWindow.Flyouts>
<controls:FlyoutsControl ItemsSource="{Binding Flyouts}">
<controls:FlyoutsControl.Resources>
<view:FlyoutPositionConverter x:Key="FlyoutPositionConverter"/>
</controls:FlyoutsControl.Resources>
<controls:FlyoutsControl.ItemTemplate>
<DataTemplate DataType="{x:Type viewModel:SettingsViewModel}">
<view:SettingsFlyout/>
</DataTemplate>
</controls:FlyoutsControl.ItemTemplate>
<controls:FlyoutsControl.ItemContainerStyle>
<Style BasedOn="{StaticResource {x:Type controls:Flyout}}"
TargetType="{x:Type controls:Flyout}">
<Setter Property="Header"
Value="{Binding Header}" />
<Setter Property="IsOpen"
Value="{Binding Visible}" />
<Setter Property="Position"
Value="{Binding Position, Converter={StaticResource FlyoutPositionConverter}}" />
<Setter Property="IsModal"
Value="{Binding IsModal}" />
<Setter Property="Theme" Value="Accent" />
</Style>
</controls:FlyoutsControl.ItemContainerStyle>
</controls:FlyoutsControl>
</controls:MetroWindow.Flyouts>
新
IFlyoutViewModel
:using System.ComponentModel;
namespace MyApplication.ViewModel
{
internal interface IFlyoutViewModel : INotifyPropertyChanged
{
string Header { get; }
bool Visible { get; set; }
Position Position { get; set; }
bool IsModal { get; set; }
}
public enum Position
{
Top,
Left,
Right,
Bottom
}
}
FlyoutPositionConverter
只是我的位置枚举和 MahApps.Metro.Controls.Position
之间的映射器因为我不想在我的 View 模型界面中使用真实的位置。此外, View 现在不再需要是
Flyout
,它可以是一个普通的用户控件。
关于c# - 带有 MVVM 的 Metro Flyout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36130858/