在事件触发WPF

时间:2016-02-19 07:26:01

标签: c# wpf telerik radgridview

我在我的RadGridView应用程序中使用了telerik WPF。其中一列具有以下功能,

当用户更改列的值时,command将作为事件触发,并显示弹出窗口。使用弹出结果(是或否)我更新collection

现在我在这里面临一个问题。

问题:

用户正在更改其中一个columnrow的值,并且在警报出现之前,他正在更改同一row的另一个column。因此,应用程序以不同的方式工作,功能崩溃。

工作尝试:

一旦事件触发,我尝试disable网格,并在功能完成后启用。但即使在事件triggers他正在改变价值之前,用户仍然非常快。

XAML:

<telerik:GridViewDataColumn Name="grdItemBuildColumn" DataMemberBinding="{Binding Build, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnlyBinding="{Binding IsEnable, Mode=OneWay, UpdateSourceTrigger= PropertyChanged}">
<telerik:GridViewDataColumn.CellEditTemplate>                                                        <DataTemplate>
<telerik:RadMaskedNumericInput Value="{Binding Build, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Mask="#1.0" Placeholder=" " 
TextMode="PlainText" AllowInvalidValues="False" IsClearButtonVisible="False" AutoFillNumberGroupSeparators="False"ext:MaskedInputExtensions.Minimum="0" SelectionOnFocus="SelectAll" AcceptsReturn="False">
<i:Interaction.Triggers>                                                                    <i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding BuidValueChangedCommand, Source={StaticResource MarketSeriesViewModel}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</telerik:RadMaskedNumericInput>
</DataTemplate>
</telerik:GridViewDataColumn.CellEditTemplate>
</telerik:GridViewDataColumn>

命令:

public ICommand BuidValueChangedCommand { get { return new RelayCommand(BuildValueChanged); } }

视图模型:

    private void BuildValueChanged()
    {
    // Ask confirmation for delete.
    if (ShowMessages.MessageBox("This will be removed from the collection", "Application"))
      {
         DeleteItem(SelectedItem.Id)
      }
    else
      {
        Item bo = RestoreBuild(SelectedItem);
        SelectedItem = bo;
      }
    }

我只需要限制用户在事件triggers之前不要更改第二个值,并从弹出窗口中选择某些内容(是/否)。

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:3)

请尝试下一个:

<强>的Xaml

<Grid>
    <telerik:RadBusyIndicator IsBusy="{Binding ImBusy, UpdateSourceTrigger=PropertyChanged}">
        <telerik:RadGridView Margin="2"
                         ItemsSource="{Binding ChannelRuleMappings}"
                         SelectionUnit="FullRow"
                         SelectionMode="Extended" AutoGenerateColumns="False"
                         IsFilteringAllowed="False">
            <telerik:RadGridView.Columns>

                <telerik:GridViewDataColumn Name="grdItemBuildColumn" DataMemberBinding="{Binding Build, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnlyBinding="{Binding IsEnable, Mode=OneWay, UpdateSourceTrigger= PropertyChanged}">
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate>
                            <telerik:RadMaskedNumericInput Value="{Binding Build, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Mask="#1.0" Placeholder="*" 
                                                       TextMode="PlainText" 
                                                           UpdateValueEvent="LostFocus"
                                                           AllowInvalidValues="False" IsClearButtonVisible="False" AutoFillNumberGroupSeparators="False" 
                                                       maskedInput:MaskedInputExtensions.Minimum="0" SelectionOnFocus="SelectAll" AcceptsReturn="False">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="ValueChanged">
                                        <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                        AncestorType={x:Type telerik:RadGridView}}, Path=DataContext.BuidValueChangedCommand}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </telerik:RadMaskedNumericInput>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>

            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </telerik:RadBusyIndicator>
</Grid>

虚拟机和型号

//GridView VM - screen is a simple implementation of the INPC
public class StackOptimizerChannelRulesViewModel : Screen
{
    //provides values for grid view items source collection
    private readonly IStackOptimizerStep _step;
    //IUserInteractionService  is a simple implementation of the massage box service
    private readonly IUserInteractionService _interactionService;
    private bool _imBusy;

    public StackOptimizerChannelRulesViewModel(IStackOptimizerStep step, IUserInteractionService interactionService)
    {
        _step = step;
        _interactionService = interactionService;
        DisplayName = "Channels Rules";
        ChannelRuleMappings = new ObservableCollection<ChannelRuleMappingModelBase>();
    }

    protected override void OnInitialize()
    {
        base.OnInitialize();
        Init();
    }

    public ObservableCollection<ChannelRuleMappingModelBase> ChannelRuleMappings { get; set; }

    //allows to show the vbusy indicator
    public bool ImBusy
    {
        get { return _imBusy; }
        set
        {
            _imBusy = value;
            NotifyOfPropertyChange(()=>ImBusy);
        }
    }

    private ICommand _cmd;
    public ICommand BuidValueChangedCommand
    {
        get { return _cmd ?? (_cmd = new ActionCommand(BuildValueChanged)); }
    }

    private void BuildValueChanged()
    {
        ImBusy = true;
        // Ask confirmation for delete.
        if (_interactionService.AskYesNo("This will be removed from the collection"))
        {
            //Add yor logic on yes
            ImBusy = false;
        }
        else
        {
            //Add yor logic on no
            ImBusy = false;
        }
    }

    private void Init()
    {
        var channelRuleMappings = _step.GetRulesForChannels();
        if (channelRuleMappings != null)
            channelRuleMappings.ForEach(parameter => ChannelRuleMappings.Add(new ChannelRuleMappingModel(parameter, _interactionService)));
    }
}

//Row VM base 
public class ChannelRuleMappingModelBase : PropertyChangedBase
{
    private string _name;
    private readonly IUserInteractionService _interactionService;
    private StackOptimizerSelectionRules _stackOptimizerSelectedRule;
    private object _build;

    public ChannelRuleMappingModelBase(string channelName, IUserInteractionService interactionService)
    {
        _name = channelName;
        _interactionService = interactionService;
    }

    public virtual string Name
    {
        get { return _name; }
    }

    public virtual StackOptimizerSelectionRules StackOptimizerSelectedRule
    {
        get { return _stackOptimizerSelectedRule; }
        set
        {
            _stackOptimizerSelectedRule = value;
            NotifyOfPropertyChange(() => StackOptimizerSelectedRule);
        }
    }

    public object Build
    {
        get { return _build; }
        set
        {
            _build = value;
            NotifyOfPropertyChange(() => Build);
        }
    }
}

//Row VM
public class ChannelRuleMappingModel : ChannelRuleMappingModelBase
{
    private StackOptimizerSelectionRules _stackOptimizerSelectedRule;
    private ISpectrumRuleParameter _ruleMapping;

    public ChannelRuleMappingModel(ISpectrumRuleParameter ruleMapping, IUserInteractionService interactionService):
        base(ruleMapping.PolarizationKey.Name, interactionService)
    {
        _ruleMapping = ruleMapping;
        _stackOptimizerSelectedRule = _ruleMapping.Rule;

    }

    public override StackOptimizerSelectionRules StackOptimizerSelectedRule
    {
        get { return _stackOptimizerSelectedRule; }
        set
        {
            _stackOptimizerSelectedRule = value;
            NotifyOfPropertyChange(() => StackOptimizerSelectedRule);
            UpdateOriginalRuleMapping(StackOptimizerSelectedRule);
        }
    }

    private void UpdateOriginalRuleMapping(StackOptimizerSelectionRules stackOptimizerSelectedRule)
    {
        if(_ruleMapping == null) return;
        _ruleMapping.Rule = stackOptimizerSelectedRule;
    }
}

小解释:

  1. 添加了一个忙碌的指示器(telerik的RadBusyIndi​​cator)。
  2. 该命令是在行的父级(RadGridView)ViewModel中定义的。
  3. 使用相对绑定来指向RadGridView的ViewModel中的命令。
  4. 每次用户更改RadMaskedNumericInput的值并将焦点移动到另一个位置(由于UpdateValueEvent =&#34; LostFocus&#34;而将标签按下或鼠标按下到其他控件) &#34; ValueChanged&#34;事件被引发触发器将启动命令,此命令将导致BusyIndi​​cator显示,BusyIndi​​cator将锁定网格视图(RadGridView)。
  5. 看起来如何: here is the picture

    如果您需要对代码进行更多解释,请与我们联系。

    问候。