如何扩展 ComboBox 以支持命令 (MVVM)?

发布于 2024-09-03 05:17:20 字数 228 浏览 2 评论 0原文

正如主题所述,我需要扩展标准 Silverlight ComboBox 的功能以支持命令。由于我遵循 MVVM,因此我需要 ComboBox 将 SelectionChanged 事件传达给我的 ViewModel。

执行此操作的代码是什么样的?我希望能够将 Command 属性放在 ComboBox XAML 控件上。

使用(WCF RIA、MVVM、VB.NET)..

所有技巧均已应用!

As topic says, I need to extend the features of a standard Silverlight ComboBox to also support Commanding. Since I follow MVVM I need my ComboBox to communicate the SelectionChanged event to my ViewModel.

What would the code look like for doing this? I want to be able to put the Command attribute on my ComboBox XAML control.

Using (WCF RIA, MVVM, VB.NET)..

All tips appricated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-09-10 05:17:20

您可以将 Combobox 的属性 SelectedIndex 或 SelectedItem 绑定到 ViewModel。所以你不需要任何命令。

示例(绑定到 SelectedIndex):

XAML

<ComboBox SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"/>

C#

public class ComboBoxViewModel
{
   private int _selectedIndex;
   public int SelectedIndex {
     get { return _selectedIndex; }
     set { 
       if (value != _selectedIndex) {
         _selectedIndex = value;
         // Perform any logic, when the SelectedIndex changes (aka. PropertyChanged-Notification)
       }
     } 
   }
}

示例(绑定到 SelectedItem):

XAML

<ComboBox SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>

C#

public class ComboBoxViewModel
{
   private MyViewModel _selectedItem;
   public MyViewModel SelectedItem {
     get { return _selectedItem; }
     set { 
       if (value != _selectedItem) {
         _selectedItem= value;
         // Perform any logic, when the SelectedIndex changes ((aka. PropertyChanged-Notification)
       }
     } 
   }
}

You can bind the property SelectedIndex or SelectedItem of the Combobox to your ViewModel. So you don´t need any Commands.

Example (Binding to SelectedIndex):

XAML

<ComboBox SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"/>

C#

public class ComboBoxViewModel
{
   private int _selectedIndex;
   public int SelectedIndex {
     get { return _selectedIndex; }
     set { 
       if (value != _selectedIndex) {
         _selectedIndex = value;
         // Perform any logic, when the SelectedIndex changes (aka. PropertyChanged-Notification)
       }
     } 
   }
}

Example (Binding to SelectedItem):

XAML

<ComboBox SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>

C#

public class ComboBoxViewModel
{
   private MyViewModel _selectedItem;
   public MyViewModel SelectedItem {
     get { return _selectedItem; }
     set { 
       if (value != _selectedItem) {
         _selectedItem= value;
         // Perform any logic, when the SelectedIndex changes ((aka. PropertyChanged-Notification)
       }
     } 
   }
}
纵情客 2024-09-10 05:17:20

创建一个公开ICommand Command对象CommandParameter 的行为。在行为中连接到 AssociatedObject 的 SelectionChanged 事件。然后,您可以将该命令绑定到您的行为并模拟 SelectionChanged 事件的命令。

Create a behavior that exposes an ICommand Command and a object CommandParameter. In the behavior wire up to the SelectionChanged event of your AssociatedObject. Then you can bind the command to your behavior and simulate a command for the SelectionChanged event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文