2009-10-09 51 views
0

我正在嘗試連接滑塊和文本框,以便文本框控制滑塊位置並將滑塊位置反映在文本框中。這是基本的雙向元素到元素綁定。我該如何解決這個問題?雙向數據綁定不起作用

我有什麼 - 滑塊位置顯示在文本框中。調整滑塊將被文本框中的更新值反映出來。這工作正如預期。

問題 - 在文本框中輸入值不會更新滑塊位置。

我在網上發現了幾個例子,並通過它們,但無法弄清楚我做錯了什麼。有什麼建議麼?

我的代碼包含在下面。

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

更新 - - -

我在共混物3建立了這個從零開始。現在它正在工作。我加了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> 

回答

0

是的,這是我會走的路。你也可以添加綁定到滑塊代替文本框並沒有UpdateSourceTrigger度日......

<Slider x:Name="slider" 
     VerticalAlignment="Bottom" 
     Height="32.8" 
     Value="{Binding ElementName=txtie, Path=Text, Mode=TwoWay}" /> 

我會保持它,你怎麼把它雖然。感覺滑塊控件是主要焦點,文本框連接到它。

此外,您的原始示例工作,只是在此方案中的默認行爲是當文本框失去焦點時而不是在屬性更改時更新綁定。

+0

謝謝!現在我知道它爲什麼似乎沒有工作。 是的,文本框被附加到滑塊。滑塊應該是主要焦點。 – DenaliHardtail 2009-10-10 18:27:38