gpt4 book ai didi

c# - 在事件触发 WPF 之前禁用网格

转载 作者:太空狗 更新时间:2023-10-29 21:49:12 27 4
gpt4 key购买 nike

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

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

现在我遇到了一个问题。

问题:

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

尝试过的工作:

我尝试在事件触发后禁用网格,并在函数完成后启用。但即使在事件触发之前,用户仍然非常快,他正在更改值。

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); } }

View 模型:

    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;
}
}

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

有人可以帮我解决这个问题吗?

最佳答案

请尝试下一个:

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. 使用相对绑定(bind)以指向 RadGridView 的 ViewModel 中的命令。
  4. 每次用户由于 UpdateValueEvent="LostFocus"更改 RadMaskedNumericInput 的值并将焦点移动到其他位置(按下选项卡或鼠标向下移动到其他控件)时,引发“ValueChanged”事件,触发器将启动命令,此命令将导致显示 BusyIndi​​cator,BusyIndi​​cator 将锁定 GridView (RadGridView)。

它的样子: here is the picture

如果您需要对代码进行更多解释,请告诉我。

问候。

关于c# - 在事件触发 WPF 之前禁用网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35499678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com