【问题标题】:The good way to implement logic, when and item is selected in ListBox (MVVM)实现逻辑的好方法,在 ListBox (MVVM) 中选择何时和项目
【发布时间】:2012-04-14 04:51:28
【问题描述】:

所以我在 Windows Phone 应用程序中实现了 MVVM(轻量级工具包)。 我有一个 SelectedItem 绑定到属性 SelectedArticle 的 ListBox。 下面是(非常简单的)属性:

private Article _selectedArticle;
public Article SelectedArticle
{
    get { return _selectedArticle; }
    set
    {
            _selectedArticle = value;
            base.RaisePropertyChanged("SelectedArticle");
    }

}

所以我想要更改视图,何时检查 ListBox 的元素。 无论如何,将视图的更改放在 settet 中很容易,但我想避免这种情况。那么该怎么做呢?

这里是 xaml:

    <ListBox IsEnabled="{Binding ListBoxEnabled, Mode=TwoWay}" SelectedItem="{Binding SelectedArticle, Mode=TwoWay}" Opacity="{Binding Opacity, Mode=TwoWay}" ItemsSource="{Binding ArticlesList}" Height="634" Width="456">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image
                        Margin="0,15"
                        VerticalAlignment="Top"
                        Source="{Binding Image}"
                        Height="100"
                        Width="100" />
                    <StackPanel>
                        <TextBlock Margin="10,15" 
                                   Width="250"
                                   TextWrapping="Wrap"
                                   VerticalAlignment="Top"
                                   HorizontalAlignment="Left"
                                   FontSize="24"
                                   Text="{Binding Content}" />
                        <TextBlock Margin="20,0"
                                   Width="100"
                                   VerticalAlignment="Top"
                                   HorizontalAlignment="Left"
                                   FontSize="20"
                                   Text="{Binding Id}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

    标签: c# windows-phone-7 mvvm mvvm-light windows-phone


    【解决方案1】:

    您想要交互触发器之类的东西吗?
    将此添加到您的 xaml

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <cmd:EventToCommand Command="{Binding EventTapCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    

    然后在你的 ViewModel 中定义 RelayCommand

    public RelayCommand EventTapCommand { get; private set; }
    public MainViewModel()
    {
        EventTapCommand = new RelayCommand(DoSomeCoolStuff);
    }
    

    如果需要,您也可以从列表中传递所选项目,您只需要设置 CommandParameter 并使用项目类型定义您的 RelayCommand。我忘记了确切的绑定语法。比如:

    <cmd:EventToCommand Command="{Binding EventTapCommand, Mode=OneWay}" CommandParameter="{Binding}"/>
    
    public RelayCommand<MyType> EventTapCommand { get; private set; }
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        看看这篇文章http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/,它使用 wpf 数据模板根据数据绑定属性显示不同的视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-07-20
          • 1970-01-01
          • 1970-01-01
          • 2011-07-01
          • 2011-09-18
          • 2012-01-31
          • 1970-01-01
          • 2023-04-07
          相关资源
          最近更新 更多