简体   繁体   English

WPF组合框的附加属性,用于不更改选区

[英]WPF Combo box attached property for slection change not firing

To fire list box selection change I have created a attached property and binding it to my XAML like following, 要触发列表框选择更改,我创建了一个附加属性,并将其绑定到我的XAML上,如下所示,

<Controls:MetroWindow x:Class="Transport.MainWindow"
          xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:extension="clr-namespace:Transport.Extensions"
        xmlns:local="clr-namespace:Transport
        mc:Ignorable="d"
        Title="CIP Simulator" Height="550" Width="1500"
                      BorderThickness="0" 
                      GlowBrush="Black"
                      ResizeMode="CanResizeWithGrip"
                      ShowMaxRestoreButton="False"
                      WindowTransitionsEnabled="False"
                      WindowStartupLocation="CenterScreen">
 <ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding Sets}" DisplayMemberPath="Set" 
                  extension:CommandProviders.Command="{Binding SetSelectionChange}"
                  SelectedItem="{Binding SelectedSet}" Grid.ColumnSpan="3"/>

Now the attached property def looks like below: 现在,附加的属性def如下所示:

public class CommandProviders
    {
        public static ICommand GetCommand(DependencyObject depObject)
        {
            return (ICommand)depObject.GetValue(CommandProprtey);
        }

        public static void SetCommand(DependencyObject depobject, ICommand value)
        {
            depobject.SetValue(CommandProprtey, value);
        }

        public static readonly DependencyProperty CommandProprtey =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandProviders), null);
    }

I have now created public I command and assigned to relayCommand (which is working relay command for other buttons). 现在,我已经创建了公共I命令并将其分配给relayCommand(对于其他按钮,该命令正在工作Relay命令)。

 public ICommand SetSelectionChange { get; set; }
 SetSelectionChange = new RelayCommand(commandx =>
                {
                    //Do Something

                });

But this is not firing up the selection change!! 但这并没有激发选择的改变!!

As mentioned in comments, you should define PropertyChangedCallback . 如评论中所述,您应该定义PropertyChangedCallback Below is the plumbing code which subscribe for combBox Selection Changed and raise the command which should trigger the bind RelayCommand . 以下是订阅combBox Selection Changed并combBox应触发绑定RelayCommand的命令的管道代码。

public class CommandProviders
{
    public static ICommand GetCommand(DependencyObject depObject)
    {
        return (ICommand)depObject.GetValue(CommandProprtey);
    }

    public static void SetCommand(DependencyObject depobject, ICommand value)
    {
        depobject.SetValue(CommandProprtey, value);
    }

    public static readonly DependencyProperty CommandProprtey =
        DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandProviders), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));

}


private static void OnCommandChanged
(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ComboBox cmbBox= (ComboBox)d;
    if (cmbBox != null)     
    {
        cmbBox.SelectionChanged += (sender, eventArgs) => 
           {
               d.GetValue(CommandProprtey)?.Invoke(null);
           }
    }
}

Few other points 其他几点

Wrote this code for your sample perspective. 出于示例角度编写了此代码。 In the code, SelectionChanged event is subscribed, so you to have to see how to unsubscribe the event so that you don't end up with memory leak. 在代码中,SelectionChanged事件已预订,因此您必须查看如何取消预订该事件,以免最终不会出现内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM