我可以同时使用PropertyChanged和LostFocus吗?

时间:2022-09-08 20:31:46

Below is a part of my View code in WPF MVVM structure.

下面是WPF MVVM结构中我的视图代码的一部分。

<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicleNo, UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" />
...                                   
<TextBlock Name="Preview" Text="{Binding EditText, UpdateSourceTrigger=PropertyChanged}"/>
...

I came across a situation where I need to use these both triggers simultaneously.

我遇到了需要同时使用这两个触发器的情况。

i.e.

即。

When I update anything in TextBox, I need to show it immediately in my Preview TextBlock. (In ViewModel EditText comes indirectly from VehicleNo itself...), which is already implemented as you can see in code.

当我更新文本框中的任何内容时,我需要立即在预览文本块中显示它。(在ViewModel EditText中间接来自车辆本身…),它已经实现了,正如您在代码中看到的那样。

And now what I need is when TextBox loose the focus, I need to validate on text.

现在我需要的是当文本框失去焦点时,我需要对文本进行验证。

Is it possible somehow to use this both properties simultaneously?

有没有可能同时使用这两个性质?

1 个解决方案

#1


0  

UpdateSourceTrigger=PropertyChanged in TextBlock has no effect

在TextBlock中更改的UpdateSourceTrigger=PropertyChanged没有效果

however you can try this way

不过你可以试试这种方法

<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" 
         Text="{Binding VehicleNo, UpdateSourceTrigger=LostFocus}" 
         HorizontalAlignment="Left" />
...                                   
<TextBlock Name="Preview" Text="{Binding Text, ElementName=VehicalNo_Text}"/>

in above sample we bind the Text property of preview TextBlock to the Text property of TextBox instead of view model property

在上面的示例中,我们将preview TextBlock的文本属性绑定到TextBox的文本属性,而不是view model属性

this will enable you to see a live preview while keeping view model update on lost focus only.

这将使您能够查看实时预览,同时仅在失去焦点时保持视图模型更新。

Edit

编辑

in order to validate the property you can apply validation rule on TextBox's binding

为了验证属性,可以对文本框的绑定应用验证规则

eg

<TextBox Name="VehicalNo_Text">
    <TextBox.Text>
        <Binding Path="VehicleNo" UpdateSourceTrigger="LostFocus">
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

you can replace ExceptionValidationRule or add with your own rules.

您可以替换ExceptionValidationRule或添加您自己的规则。

secondly instead of heaving a calculated property you may perhaps use a converter to display the preview based on VehicleNo's value

其次,您可以使用转换器根据车流的值显示预览,而不是使用计算的属性

eg

<TextBlock Name="Preview" Text="{Binding Text, ElementName=VehicalNo_Text, Converter={StaticResource VehicalNoConverter}}"/>

#1


0  

UpdateSourceTrigger=PropertyChanged in TextBlock has no effect

在TextBlock中更改的UpdateSourceTrigger=PropertyChanged没有效果

however you can try this way

不过你可以试试这种方法

<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" 
         Text="{Binding VehicleNo, UpdateSourceTrigger=LostFocus}" 
         HorizontalAlignment="Left" />
...                                   
<TextBlock Name="Preview" Text="{Binding Text, ElementName=VehicalNo_Text}"/>

in above sample we bind the Text property of preview TextBlock to the Text property of TextBox instead of view model property

在上面的示例中,我们将preview TextBlock的文本属性绑定到TextBox的文本属性,而不是view model属性

this will enable you to see a live preview while keeping view model update on lost focus only.

这将使您能够查看实时预览,同时仅在失去焦点时保持视图模型更新。

Edit

编辑

in order to validate the property you can apply validation rule on TextBox's binding

为了验证属性,可以对文本框的绑定应用验证规则

eg

<TextBox Name="VehicalNo_Text">
    <TextBox.Text>
        <Binding Path="VehicleNo" UpdateSourceTrigger="LostFocus">
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

you can replace ExceptionValidationRule or add with your own rules.

您可以替换ExceptionValidationRule或添加您自己的规则。

secondly instead of heaving a calculated property you may perhaps use a converter to display the preview based on VehicleNo's value

其次,您可以使用转换器根据车流的值显示预览,而不是使用计算的属性

eg

<TextBlock Name="Preview" Text="{Binding Text, ElementName=VehicalNo_Text, Converter={StaticResource VehicalNoConverter}}"/>