代码之家  ›  专栏  ›  技术社区  ›  Padu Merloti

来自itemscontrol item的relaycommand sender

  •  1
  • Padu Merloti  · 技术社区  · 16 年前

    我已经成功地使用了MVVM的relaycommand将操作绑定到XAML,但是我的itemsControl有一个小问题。

        <ItemsControl ItemsSource="{Binding Devices}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Width="100" Margin="4" >
                        <Button Command="{Binding Path=SelectDeviceCommand}" >
                            <Grid>
                                <Image Source="img_small.png"></Image>
                                <Image Source="{Binding Path=Logo}" />
                            </Grid>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    

    在我的视图模型中:

        public RelayCommand SelectDeviceCommand { get; set; }
        private ObservableCollection<Device> Devices;
    
        Devices = CreateListOfDevices();
    
        private void InitializeCommands()
        {
            SelectDeviceCommand = new RelayCommand((s) => MessageBox.Show(s.ToString()));
        }
    

    如何在视图模型中定义selectdevicecommand以接收绑定到该项的对象?

    甚至没有调用我的SelectDeviceCommand…(但我想这是因为我需要使我的设备成为迷你视图模型并在其中实现selectdevicecommand,对吗?)

    3 回复  |  直到 14 年前
        1
  •  4
  •   LBugnion    16 年前

    如果使用类似于MVVM Light应用程序中的ViewModelLocator,则可以从数据模板中获取对MainViewModel的引用,其中

    <Button Command="{Binding Main.SelectDeviceCommand, Source={StaticResource Locator}}">
    

    我发现这种方法比elementname 1更干净,但是它当然假定主属性在定位器中可用,并且MainViewModel被实例化为一个单例。显然,这并不总是可能的。在这种情况下,我认为elementname变通方法可以接受。

    在wpf中,您也可以使用模式为findancestor的relativesource,但我发现它更混乱;)

    关于“如何在视图模型中定义selectdevicecommand以接收绑定到该项的对象?”的问题。,我不能百分之百确定我理解这个问题,但是如果您想要得到由datatemplate表示的项(在本例中是设备),则应该使用commandParameter:

    <Button Command="{Binding Main.SelectDeviceCommand, Source={StaticResource Locator}}"
            CommandParameter="{Binding}"}">
    

    干杯, 劳伦特

        2
  •  0
  •   Matt Hamilton    16 年前

    是的,我打过这个。我见过一些人使用一个定制的“commandreference”类来解决这个问题,他们将该类作为一个资源添加到窗口中,但我无法使其发挥作用。

    最后,我使用元素绑定到窗口(或页面)本身,因为ViewModel是窗口的DataContext。首先,给窗口(或页面)命名:

    <Window ...
        x:Name="me" />
    

    然后直接绑定到窗口的DataContext,如下所示:

    <Button Command="{Binding DataContext.SelectDeviceCommand,ElementName=me}">
    

    这对我很有效。它很乱,但我觉得它可读性很好。

        3
  •  0
  •   robert vano    14 年前

    我有一个usercontrol(x:name=“controlClass”),它在一个模板内,不能在XAML上工作。 我这样称呼它

    <Button Content="New1" Command="{Binding DataContext.NewTabCommand,ElementName=ControlClass}"/>
    
    
    
    namespace Doit_Project.Modules.Tasks.ViewModels
    {
        [Export]
        public class WindoTabViewModel : Doit_ProjectViewModelBase
        {
            public WindoTabViewModel()
            {
    
            }
    
            private RelayCommand _newTabCommand;
            public RelayCommand NewTabCommand
            {
                get { return _newTabCommand ?? (_newTabCommand = new RelayCommand(OnNewTab)); }
            }
    
            public void OnNewTab()
            {
    
            }
        }
    }