小编Mil*_*rov的帖子

WPF/XAML - 绑定到 TextBox PreviewTextInput 事件

我正在尝试将文本框“PreviewTextInput”绑定到视图模型中的方法。我正在关注这篇文章 ,但我的方法从未被调用。这是我的 XAML 代码:

<UserControl x:Class="ConfigurationView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:OPCUAProjectModule.Views"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="700">
.....
.....
.....
                    <TextBox x:Name="txtServer" Text="{Binding Server, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="PreviewTextInput" >
                                <i:InvokeCommandAction Command="{Binding IsAllowedInput}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TextBox>
....
....
Run Code Online (Sandbox Code Playgroud)

这里我们使用 ViewModel 代码:

public class ConfigurationViewModel : BindableBase, INotifyDataErrorInfo
{
....
....
    public string Server
    {
        get
        {
            return this.server;
        }

        set
        {
            this.SetProperty(ref this.server, value);
        }
    }

    private void IsAllowedInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
    {
        //Never enters here.
    }
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml prism mvvm

6
推荐指数
1
解决办法
5514
查看次数

如何避免在foreach IList之前进行空检查

我有以下代码:

IList<object> testList = null;

... 

if (testList != null) // <- how to get rid of this check?
{
   foreach (var item in testList)
   {
       //Do stuff.
   }
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法,以避免if之前foreach?我看到了一些解决方案,但是使用时List,有解决方案IList吗?

c# null-check

2
推荐指数
2
解决办法
90
查看次数

标签 统计

c# ×2

mvvm ×1

null-check ×1

prism ×1

wpf ×1

xaml ×1