我正在尝试使用 Fluent 功能区创建一个下拉按钮,以显示数据库中所有可用表的菜单。菜单是通过使用从数据库中检索到 TablesList 的列表生成的,然后将其绑定到下拉按钮的 itemssource。
到目前为止一切顺利,菜单已生成并且表名显示正确。
现在我想将每个菜单项绑定到一个命令,该命令将使用命令 LoadTableCommand 将它们加载到 CurrentTable 中,该命令将获取当前菜单项的标题并使用它来加载正确的表。
这是问题出现的地方,命令没有执行,我似乎没有得到任何错误或线索来说明为什么会(或没有)发生。
我现在唯一能想到的可能是在视图模型本身中绑定命令,然后将其分配给 ItemsSource,但我还没有弄清楚我将如何做到这一点,因为我最喜欢的搜索引擎还没有找到解决方案我..
PS:DataManager 类可以正常工作,如果这可能与您有关的话。
有人知道我在哪里出错了吗?
一如既往,任何帮助将不胜感激;)
主窗口.xaml
...
<Fluent:DropDownButton Header="Table"
ItemsSource="{Binding TablesList}">
<Fluent:DropDownButton.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding LoadTableCommand}" />
<Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=Header}" />
</Style>
</Fluent:DropDownButton.ItemContainerStyle>
</Fluent:DropDownButton>
...
主视图模型.cs
...
private DataManager dataManager = new DataManager("Data Source=db.sqlite");
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
TablesList = dataManager.GetTables();
CurrentTable = dataManager.GetTable("PriceList");
}
/// <summary>
/// The <see cref="TablesList" /> property's name.
/// </summary>
public const string TablesListPropertyName = "TablesList";
private List<string> _tablesList = new List<string>();
/// <summary>
/// Sets and gets the TablesList property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public List<string> TablesList
{
get
{
return _tablesList;
}
set
{
if (_tablesList == value)
{
return;
}
RaisePropertyChanging(TablesListPropertyName);
_tablesList = value;
RaisePropertyChanged(TablesListPropertyName);
}
}
/// <summary>
/// The <see cref="CurrentTable" /> property's name.
/// </summary>
public const string CurrentTablePropertyName = "CurrentTable";
private DataTable _currentTable = new DataTable();
/// <summary>
/// Sets and gets the CurrentTable property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public DataTable CurrentTable
{
get
{
return _currentTable;
}
set
{
if (_currentTable == value)
{
return;
}
RaisePropertyChanging(CurrentTablePropertyName);
_currentTable = value;
RaisePropertyChanged(CurrentTablePropertyName);
}
}
private RelayCommand<string> _loadTable;
/// <summary>
/// Gets the LoadTableCommand.
/// </summary>
public RelayCommand<string> LoadTableCommand
{
get
{
return _loadTable
?? (_loadTable = new RelayCommand<string>(
table => LoadTable(table)));
}
}
private void LoadTable(string name)
{
Console.WriteLine(name);
// CurrentTable = dataManager.GetTable(name);
}
...