堆叠组合框选定的项目属性执行 [英] Stacking up combo box selected item property execution

查看:49
本文介绍了堆叠组合框选定的项目属性执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不寻常的问题,让我们说我有一个主要的winow应用程序:



现在您看到我将窗口拆分为两个第一部分导航,其中的按钮可以更改视图,另一部分则显示视图,在MainWindowsView模型中使用Mvvm,它看起来像xaml:

  < StackPanel     DockPanel    Dock   =  " Top"  >   
<按钮 内容 = " View 1" 命令 = " {Binding View1ViewModel}" />
< span class ="tag"><按钮 内容 = " View 2" 命令 = " {Binding View2ViewModel}" />
<按钮 内容 = " View 3 w combobox" 命令 = " {Binding ListComboboxViewModel}" />
< / StackPanel>

<网格>
< ContentControl 网格 = " 0" DataContext = " {Binding ContentControlDataContext}" 内容 = " {Binding}" />
< / Grid>

查看模型:

   private     readonly     View1ViewModel   view1ViewModel  ;   
private readonly View2ViewModel view2ViewModel ;
私人 readonly ListComboboxViewModel listComboboxViewModel ;

public ICommand SetView1ViewModel { get ; set ; }
public ICommand SetView2ViewModel { get ; set ; }
public ICommand SetListComboboxViewModel { get ; set ; }

public object ContentControlDataContext
{
get => this contentControlDataContext ;
set
{
contentControlDataContext = value ;
OnPropertyChanged ();
}
}

public MainViewModel ()
{
view1ViewModel = new View1ViewModel ();
view2ViewModel = new View2ViewModel ();
listComboboxViewModel = new ListComboboxViewModel ();

this SetView1ViewModel = new 命令 p => ContentControlDataContext = view1ViewModel );
this View2ViewModel = new 命令 p => ContentControlDataContext = view2ViewModel );
< span class ="typ"> SetListComboboxViewModel = new 命令 p => ContentControlDataContext = listComboboxViewModel );
}

基本上一切在这个结构上工作,我按下按钮查看更改。问题是在视图3中有一个组合框xaml:

  < ComboBox     ItemsSource   =  " {Binding ProjectsBrowserAxmModules}"     SelectedValuePath   =  " AxmModuleId"     DisplayMemberPath   =  " AxmModuleName"   
SelectedValue = " {Binding SelectedAxmModule,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}"
控件:TextBoxHelper 水印 = " {Binding BrowserComboBoxWatermark}" 高度 = " 2" IsSynchronizedWithCurrentItem = " True" />

这是绑定到属性的:

     private     int    selectedAxmModule  ;   
public int SelectedAxmModule
{
get => selectedAxmModule ;
set
{
selectedAxmModule
= value ;
OnPropertyChanged ();

GetCurrentsModule (); //在组合框中选择值后,此方法会在视图中进行午餐和更新列表。
}
}

现在一切正常,当我使用组合框对mu视图进行操作时,但是当我使用左边的按钮移动到其他视图时,我感觉很怪异,看起来所选项目上的属性正在储存。例如从我的组合框视图i
移动到视图1然后查看2并查看1然后列表并选择值,此属性将设置(ans午餐方法)这么多次,因为我改变了我的视图,我找不到原因。我怀疑它可能是我的命令实现
看起来像:

   public     class    命令       ICommand   
{
private readonly 操作 < object> 执行 ;

private readonly 谓词 <对象> canExecute ;

public 命令 < span class ="typ">操作
< object> 执行 谓词 < object> canExecute = null
{
执行 = 执行 ;
this canExecute = canExecute ;
}

public bool CanExecute object 参数
{
返回 canExecute == null || canExecute != null && ; canExecute 参数 ));
}

public void 执行 object 参数
{
执行 参数 );
}


public 事件 EventHandler CanExecuteChanged
{ < span class ="pln">
add => CommandManager RequerySuggested + = value ;
remove
=> CommandManager RequerySuggested - = ;
}
}

但不确定,有没有人有类似的经历?



解决方案

我找到了答案,它看起来像每个视图渲染一些控件事件都堆叠起来,所以我在xaml中删除了绑定它与事件和:

  private    void   ComboBox_CurrentBrowserAxmChanged(对象 发件人,   RoutedEventArgs   e)
{
     < span style ="color:blue"> var   combo  = ( ComboBox )发件人;

  ;    if  (combo !=  null  & &  combo.IsDropDownOpen)
     { 
        (( CurrentsHistoryViewModel )DataContext).GetCurrentsModuleCommand.Execute(sender);
         combo.IsDropDownOpen  =  < span style ="color:blue"> false ;
    }
}


I have an unusual problem, let us say i have an application that main winow would look like:

Now as you see i have window split into two section one navigation with buttons that change view, other where view is displayed, using Mvvm in MainWindowsView model it looks like xaml:

<StackPanel DockPanel.Dock="Top">
  <Button Content="View 1" Command="{Binding View1ViewModel}"/>
  <Button Content="View 2" Command="{Binding View2ViewModel}" />
  <Button Content="View 3 w combobox" Command="{Binding ListComboboxViewModel}" />
 </StackPanel>

 <Grid>
   <ContentControl Grid.Row="0" DataContext="{Binding ContentControlDataContext}" Content="{Binding}" />
 </Grid>

View model:

private readonly View1ViewModel view1ViewModel;
private readonly View2ViewModel view2ViewModel;
private readonly ListComboboxViewModel listComboboxViewModel ;

public ICommand SetView1ViewModel { get; set; }
public ICommand SetView2ViewModel { get; set; }
public ICommand SetListComboboxViewModel{ get; set; }

public object ContentControlDataContext
{
   get => this.contentControlDataContext;
   set
   {
      this.contentControlDataContext = value;
      OnPropertyChanged();
   }
 }

public MainViewModel()
{
    this.view1ViewModel= new View1ViewModel();
    this.view2ViewModel= new View2ViewModel();
    this.listComboboxViewModel= new ListComboboxViewModel();

   this.SetView1ViewModel = new Command(p => ContentControlDataContext = this.view1ViewModel);
   this.View2ViewModel = new Command(p => ContentControlDataContext = this.view2ViewModel);
   this.SetListComboboxViewModel= new Command(p => ContentControlDataContext = this.listComboboxViewModel);
}

And basically everything works on this construction, I press button view changes. Problem is with view 3 there is a combobox there xaml:

<ComboBox ItemsSource="{Binding ProjectsBrowserAxmModules}" SelectedValuePath="AxmModuleId" DisplayMemberPath="AxmModuleName"  
    SelectedValue="{Binding SelectedAxmModule, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
    controls:TextBoxHelper.Watermark="{Binding BrowserComboBoxWatermark}" Height="2" IsSynchronizedWithCurrentItem="True" />

And this is binded to a property:

    private int? selectedAxmModule;
    public int? SelectedAxmModule
    {
        get => selectedAxmModule;
        set
        {
            selectedAxmModule = value;
            OnPropertyChanged();

            GetCurrentsModule(); //after selecting value in combobox this method lunches and updates list in view.
        }
    }

Now everything works fine when i do operation on mu view with combobox, but when i move to other view using buttons on the left I am getting weird situation, it looks like that property on selected item is stocking up. for example from my combobox view i move to view 1 then to view 2 and to view 1 back then to list and select value, this property will set (ans lunch method as well) so many times as i changed my view, and i can not find reason why. I have a suspicion it might be something with my command implementation that looks like:

public class Command : ICommand
{
    private readonly Action<object> execute;

    private readonly Predicate<object> canExecute;

    public Command(Action<object> execute, Predicate<object> canExecute = null)
    {
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return this.canExecute == null || (this.canExecute != null && this.canExecute(parameter));
    }

    public void Execute(object parameter)
    {
        this.execute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add => CommandManager.RequerySuggested += value;
        remove => CommandManager.RequerySuggested -= value;
    }
}

But not sure, anyone had similar experience??

解决方案

I found the answer, it looks like on every view rendering for some controls events are stack up, so i removed binding in xaml replaced it with event and :

private void ComboBox_CurrentBrowserAxmChanged(object sender, RoutedEventArgs e)
{
    var combo = (ComboBox)sender;
 
    if (combo != null && combo.IsDropDownOpen)
    { 
        ((CurrentsHistoryViewModel)DataContext).GetCurrentsModuleCommand.Execute(sender);
        combo.IsDropDownOpen = false;
    }
}


这篇关于堆叠组合框选定的项目属性执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆