【发布时间】:2009-10-09 14:43:47
【问题描述】:
我正在尝试连接滑块和文本框,以便文本框控制滑块位置并且滑块位置反映在文本框中。这是基本的双向元素到元素绑定。
我所拥有的 - 滑块位置显示在文本框中。调整滑块会通过文本框中的更新值反映出来。这按预期工作。
问题 - 在文本框中输入值不会更新滑块位置。
我在网上找到了一些示例并浏览了它们,但无法弄清楚我做错了什么。有什么建议吗?
我的代码包含在下面。
<Window x:Class="ScrollBarBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="756">
<Grid Margin="10">
<Grid.Resources>
<Style x:Key="txtStyle" TargetType="TextBox">
<Setter Property="FontSize" Value="15pt"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="400"/>
</Grid.ColumnDefinitions>
<TextBox Name="txtie" Grid.Column="0" Style="{StaticResource txtStyle}" Text="{Binding ElementName=scrollie, Path=Value, Mode=TwoWay}" />
<ScrollBar Name="scrollie" Orientation="Horizontal" Grid.Column="1" Minimum="0" Maximum="10" />
</Grid>
</Window>
更新 - - -
我在 Blend3 中从头开始构建它。现在它正在工作。我添加了 UpdateSourceTrigger=PropertyChanged。对于这是否是最佳解决方案,我仍然愿意接受评论或建议。
<Grid x:Name="LayoutRoot">
<TextBox HorizontalAlignment="Left"
Margin="97.293,121.6,0,0"
VerticalAlignment="Top"
Width="139.2"
Height="40.8"
Text="{Binding Value, ElementName=slider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"/>
<Slider x:Name="slider"
Margin="97.293,0,194.707,140.8"
VerticalAlignment="Bottom"
Height="32.8"/>
</Grid>
【问题讨论】: