2014-02-19 34 views
1

目前我有以下綁定:兩種綁定在一個TextBox上?

 <TextBox Text="{Binding Mode=TwoWay, Path=SearchText, UpdateSourceTrigger=LostFocus}"> 

在代碼的時候,因此觸發設置爲LostFocus我執行昂貴,長時間運行的操作,以搜索文本的變化只執行在用戶點擊輸入或標籤後。

但是,只要該文本框中的文本不是空的,我想另外啓用另一個控件。

我可以在TextBox中有第二個綁定,或者直接在XAML中將其他控件綁定到我的文本框的當前內容。

回答

1

首先默認值的文本DP的UpdateSourceTriggerLostFocus你沒有這樣做手動。另外,Text DP的默認模式是TwoWay,您也可以省略該模式。

其次,UpdateSourceTrigger表示綁定的源值應更新哪個操作。所以,你肯定可以啓用/禁用另一個控件,並且你需要綁定另一個控件。

這將起作用,因爲當源屬性即文本DP被更改時,DP的綁定(即,IsEnabled)被更新。

<StackPanel> 
     <TextBox x:Name="txt" Text="{Binding SearchText}"/> 
     <Button Content="TestButton" 
       IsEnabled="{Binding ElementName=txt, Path=Text, 
          Converter={StaticResource StringToBoolConverter}}"/> 
    </StackPanel> 
+0

甜!如果我也綁定了一個命令,那麼'IsEnabled'綁定會優先於命令的啓用設置嗎? – cacau

+0

不,在這種情況下'CommandManager'將覆蓋'IsEnabled'的值。理想的啓用/禁用代碼應該在一個地方。而且,它取決於最後調用哪一個。如果'IsEnabled'綁定被最後評估,它將覆蓋commandManager的CanExecute值,反之亦然。所以,最後一個會永遠贏得並覆蓋價值。 –

+0

好的,謝謝!必須使用'click'而不是命令,然後 – cacau

1

您可以將其他控件綁定到TextBoxText屬性。與其他的控制將立即受到影響時Text性質改變,甚至在LostFocus事件引發:

<TextBox x:Name="txtSearch" Text="{Binding Mode=TwoWay, Path=SearchText, UpdateSourceTrigger=LostFocus}"> 
<TextBlock Text="{Binding Path=Text, ElementName=txtSearch}"/> 
0

喜不使用綁定了太多我自己,我reasonly做了一個小實驗multibinding剛剛結識的結合概念有點苦澀。

這應該澄清一點如何結合可以爲您的工作優勢:

<StackPanel> 
    <Slider Name="SliderFontSize" Margin="3" Minimum="1" Maximum="40" Value="10" TickFrequency="1" TickPlacement="TopLeft" IsSnapToTickEnabled="True" /> 
    <TextBox Name="TextBoxTextContent" Margin="5" Text="Denne tekst er en test" /> 
    <ListBox Name="ListBoxColors" BorderBrush="Black" Margin="5" > 
     <ListBoxItem Tag="Red" Content="Red" ></ListBoxItem> 
     <ListBoxItem Tag="Yellow" Content="Yellow" ></ListBoxItem> 
     <ListBoxItem Tag="Green" Content="Green" ></ListBoxItem> 
    </ListBox> 
    <TextBlock Margin="3" Name="TextBlockSampleText" Height="50" FontSize="{Binding ElementName=SliderFontSize, Path=Value, Mode=TwoWay}" 
       Text="{Binding ElementName=TextBoxTextContent, Path=Text}" Foreground="{Binding ElementName=ListBoxColors, Path=SelectedItem.Tag}" /> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Name="TextBlockExactSize" Text="Exact Size:" VerticalAlignment="Center" /> 
     <!-- I .Net 4.5 er der mulighed for at smide en delay property på som starter hvergang der er foretaget en ændring, men updater først 
      sourcen når delay tiden er nået. og resetter hver gang der foretages en ny ændring. Delay=500 (halvt sekundt) --> 
     <TextBox Name="TextBoxExactSize" Text="{Binding ElementName=TextBlockSampleText, Path=FontSize, Mode=TwoWay, UpdateSourceTrigger=Explicit}" 
       HorizontalAlignment="Left" Width="100" Margin="10 5" TextChanged="TextBoxExactSize_TextChanged" /> 
    </StackPanel> 
    <Button Name="ButtonClose" IsCancel="True" Content="Close" HorizontalAlignment="Right" Height="30" Width="100" Margin="5" Click="ButtonClose_Click" /> 
</StackPanel> 

在後面的代碼:如果UpdateSourceTrigger設置爲「顯性」

private void TextBoxExactSize_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    pm_RetrievedBindingExpression = TextBoxExactSize.GetBindingExpression(TextBox.TextProperty); 
    pm_RetrievedBindingExpression.UpdateSource(); 
} 

//此方法所必要的。這樣,您可以在事件中執行一些代碼,並在方法完成其工作時更新源代碼。如果您不喜歡這種行爲,您可以將Explicit更改爲PropertyChanged,它會在發生更改時立即更新您的屬性。

只要創建一個新的窗口c/p它並玩弄它。希望這有助於:)