gpt4 book ai didi

MVVM - 在对象列表中,每个对象都包含一个命令,我怎样才能获得适当的命令来触发?

转载 作者:行者123 更新时间:2023-12-03 10:51:04 25 4
gpt4 key购买 nike

也许我的做法是错误的,但在我的 View 模型中,我有一个对象列表(CGCAppSwitchboardItem),每个对象都包含一个 DelegateCommand 属性。我的意图是列表中的每个项目 (CGCAppSwitchboardItem) 将代表我的解决方案中的一个模块,从而调用该命令将加载一个模块。或者,更好的说法是,我正在使用这个列表来构建一个按钮或菜单项列表,这些按钮或菜单项将在选择时打开请求的 View 。

我遇到的问题是最后一个 CGCAppSwitchboardItem 的命令是不管按下按钮的命令。我已经通过以不同的顺序添加它们来测试这一点,无论最后添加哪个,这就是被触发的命令。我怎样才能得到适当的命令来触发?

编辑:我已经缩小了问题范围,不知道该怎么做。问题出现在我将其分配给 RunMethod 时创建的通用函数中。如果您查看下面的代码,我会在创建 CGCAppSwitchboardItem 类时分配 RunMethod。如果您查看我的通用函数,我将传入 itm.ModuleName 属性。显然,新创建的委托(delegate),即我分配给 RunMethod 的委托(delegate),保留了对 itm.ModuleName 的引用,而不是我想要的字符串值。由于这是一个循环,因此 itm 变量仍然设置为列表中的最后一个模块。因此,当调用 RunMethod() 时,它引用 itm.ModuleName 而不是“MyModuleName”。有任何想法吗?

public class CGCAppSwitchboardItem : ISwitchboardItem
{
public Action RunMethod { get; set; } //RunMethod gets assigned by external ViewModel

public ICommand ExecuteCommand { get; set; }

public CGCAppSwitchboardItem()
{
ExecuteCommand = new DelegateCommand<object>(
o =>
{
if (RunMethod != null)
{
RunMethod();
}
},
o =>
{
return (RunMethod != null);
});
}
}


public class CGCApplicationShellViewModel : ICGCApplicationShellViewModel, INotifyPropertyChanged, ISwitchboardListContainer
{

//...为简洁起见删除了代码
私有(private)无效 PopulateSwitchboardItems()
{
如果(_moduleCatalog != null)
{
_switchBoardItems.Clear();
            foreach (var itm in _moduleCatalog.Modules)
{
_switchBoardItems
.Add(
new CGCAppSwitchboardItem()
{
Name = itm.ModuleName,
RunMethod = () =>
{

System.Windows.Forms.MessageBox.Show(itm.ModuleName);
_moduleManager.LoadModule(itm.ModuleName);
//_moduleManager.LoadModule("ATimesheetModule");
}
}
);
}
}
}
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:WTSSwitchBrdItm="clr-namespace:WTS.CGCApplicationInterface.Switchboard"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="150">
<UserControl.Resources>
<DataTemplate x:Key="ShowModule">
<Button Name="btnOpenView" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto"
Command="{Binding ExecuteCommand}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="15" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image x:Name="imgAvatar" Source="{Binding Path=Avatar}" Grid.Column="0" Grid.Row="0" Width="50" Height="50"/>
<TextBlock x:Name="txtName" Text="{Binding Path=Name}" Grid.Column="1" Grid.Row="0" />
<TextBlock x:Name="txtDescription" Margin="0, 5, 0, 5" Grid.Column="1" Grid.Row="3" >
<TextBlock.Text>
<MultiBinding StringFormat=" ({0})">
<Binding Path="Description"/>

</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Button>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding Path=SwitchboardItems}" ItemTemplate="{StaticResource ShowModule}"></ListBox>
</Grid> </UserControl>

最佳答案

您可以包含 CommandParameter 来识别记录:

<Button Command="{Binding ExecuteCommand}" CommandParameter="{Binding RecordId}" />

关于MVVM - 在对象列表中,每个对象都包含一个命令,我怎样才能获得适当的命令来触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3320326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com