gpt4 book ai didi

wpf - 绑定(bind)延迟属性 WPF - 我没有注意到任何差异

转载 作者:行者123 更新时间:2023-12-02 04:40:48 25 4
gpt4 key购买 nike

我试图更多地了解绑定(bind)延迟及其影响。我已经实现了一个简单的代码,但是,老实说,我最终没有注意到任何视觉差异,无论有没有延迟。这是代码:

<Window x:Class="Example00.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Grid >
<Grid.RowDefinitions >
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Name="mySourceElement" Grid.Row="0" >Hello World</TextBox>
<TextBlock Grid.Row="1">
<TextBlock.Text>
<Binding ElementName="mySourceElement" Path="Text" Mode="TwoWay" Delay="60000" />
</TextBlock.Text>
</TextBlock>

<TextBlock Text="{Binding ElementName=mySourceElement, Mode=TwoWay, Path=Text, Delay=50000}" Grid.Row="2" />
</Grid>

它基本上是基于代码项目教程的代码(http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part - 示例零),但使用 .Net 4.5 并添加了延迟。我添加了很长的延迟来直观地看到差异,但是我没有注意到与不使用延迟有什么不同。

我想知道我是否误解了该属性 - 其他文本框中的文本不应该等待“延迟”量以反射(reflect)用户在第一个文本框中键入的更改吗?

最佳答案

是的,您有点误解了延迟。该属性的命名方式非常令人困惑。事实上,它只有一种方式,从目标到源。这意味着当目标中发生每次更改时,更新到源的更改都会被延迟。另一种方法行不通,这意味着源中发生的每次更改都不会延迟对目标的反射。

所以在这种情况下应该是这样的:

<!-- NOTE: we name TextBlock as target but 
in fact it's the source of the Binding -->
<TextBox Text="{Binding Text, ElementName=target, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, Delay=1000}"
></TextBox>
<TextBlock Grid.Row="1" Name="target">
</TextBlock>

在您的代码中,您的Binding 的源为TextBox,目标为TextBlock。因此,TextBox 中的每个更改都会立即反射(reflect)到 TextBlock,而不会受到 Delay 的影响。

关于wpf - 绑定(bind)延迟属性 WPF - 我没有注意到任何差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32888174/

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