2011-10-25 114 views
1

我有2個文本框,每個在不同的列表視圖。第一個文本框應該顯示來自xml文件的數據。所以當我點擊文本框時,第一個文本框中的數據將顯示在第二個文本框中。我做了一個非常大的循環,獲取特定的對象,當我點擊它並追加到另一個列表視圖。有沒有更簡單的方法來通過xaml中的元素名稱進行綁定來完成此操作?我在textbox1中的elementName將是textbox2的名稱。我嘗試這樣做,但我不確定我的路徑應該是什麼?數據綁定爲兩個文本框

抱歉,不包括我的xaml。

<Window x:Class="GridViewTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" 
    xmlns:local="clr-namespace:GridViewTest" 
    Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="541" d:DesignWidth="858" SizeToContent="WidthAndHeight"> 
<Window.Resources> 
    <local:PacketList x:Key="PacketList"/> 
    <local:BindableSelectionTextBox x:Key="BindableSelectionTextBox"/> 
</Window.Resources> 
<Grid Height="500" Width="798"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="142*" /> 
     <RowDefinition Height="145*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="234*" /> 
     <ColumnDefinition Width="233*" /> 
    </Grid.ColumnDefinitions> 
    <ListView ItemsSource="{Binding}" x:Name="lvItems" Grid.RowSpan="2" Grid.ColumnSpan="2"> 
     <ListView.View> 
      <GridView AllowsColumnReorder="True"> 
       <GridViewColumn Header="Header" Width="200"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <Grid> 
           <TextBox Name ="A" Tag="Header" Text="{Binding SelectedText, Path=headerObj.headervalue}" PreviewMouseLeftButtonUp="Handle_Click" 
             IsReadOnly="True" BorderThickness="0" > 
           </TextBox> 
          </Grid> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
     </ListView.View> 
    </ListView> 
    <ListView Margin="0,245,0,8" Grid.ColumnSpan="2" Grid.RowSpan="2" > 
     <TextBox Name="headText" Text="{Binding SelectedText,ElementName=A}"/> 
    </ListView>  
</Grid> 

+0

兩個文本框是在同一個容器/用戶控件/窗口中嗎? – benPearce

+0

是的,他們在同一個容器/用戶控件/窗口中 – edelweiss

+0

你可以發佈你的xaml嗎? – wdavo

回答

1

首先讓我們對WPF中的NameScoping進行一些教育。在WPF中,Templates中的任何綁定的範圍僅限於Template。此外,在模板內命名的任何元素都不可用於模板外部的Binding.ElementName引用。

所以你的情況的TextBox A不能用文本框headText爲文本框A稱作是名作用域GridViewColumn.CellTemplate下。

又是爲什麼是headText文本框下一個ListViewItemsControls like ListBoxListViewDataGrid不應該用作面板或容器來承載單個元素。他們的意圖是顯示多個項目。改爲使用PanelContentControl

<Grid Margin="0,245,0,8" Grid.ColumnSpan="2" Grid.RowSpan="2" > 
     <TextBox Name="headText" Text="{Binding SelectedText,ElementName=A}"/> 
    </Grid> 

OR

<ContentControl Margin="0,245,0,8" Grid.ColumnSpan="2" Grid.RowSpan="2" > 
     <TextBox Name="headText" Text="{Binding SelectedText,ElementName=A}"/> 
    </ContentControl> 

我們之間兩個文本框使用下面的技巧選擇同步...

XAML

<TextBox Name="SelectionSource" 
      Tag="{Binding ElementName=SelectionTarget}" 
      SelectionChanged="SelectionSource_SelectionChanged" /> 
    <TextBox Name="SelectionTarget" 
      Text="{Binding SelectedText, ElementName=SelectionSource, 
          Mode=TwoWay, UpdateSourceTrigger=Explicit}" /> 

代碼隱藏...

private void SelectionSource_SelectionChanged(object sender, RoutedEventArgs e) 
    { 
     var targetTextBox = ((TextBox) sender).Tag as TextBox; 
     if (targetTextBox != null) 
     { 
      var bndExp 
       = BindingOperations.GetBindingExpression(
        targetTextBox, TextBox.TextProperty); 

      if (bndExp != null) 
      { 
       bndExp.UpdateTarget(); 
      } 
     } 
    } 

如果您使用的MVVM然後處理在附加的行爲此SelectionSource_SelectionChanged事件。

編輯2:

現在的情況下,如果一個文本框列表框模板的一部分等在外面的模板,然後使用內容控制黑客......

XAML:

<Window.Resources>  
    <TextBox x:Key="SelectionTarget" 
      Text="{Binding Tag.SelectedText, 
          RelativeSource={RelativeSource Self}, 
          Mode=TwoWay, 
          UpdateSourceTrigger=Explicit}" /> 
    </Window.Resources> 

    <StackPanel> 
    <ListBox> 
     <ListBox.ItemsSource> 
      <x:Array Type="{x:Type System:String}"> 
       <System:String>Test String 1</System:String> 
       <System:String>Test String 2</System:String> 
       <System:String>Test String 3</System:String> 
      </x:Array> 
     </ListBox.ItemsSource> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBox Name="SelectionSource" 
         Text="{Binding Path=., Mode=TwoWay}" 
         Tag="{StaticResource SelectionTarget}" 
         SelectionChanged="SelectionSource_SelectionChanged" /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

    <ContentControl Content="{StaticResource SelectionTarget}">   
    </ContentControl> 

</StackPanel> 

代碼

背後
private void SelectionSource_SelectionChanged(
     object sender, RoutedEventArgs e) 
    { 
     var targetTextBox 
      = ((TextBox) sender).Tag as TextBox; 
     if (targetTextBox != null) 
     { 
      targetTextBox.Tag = (TextBox) sender; 

      var bndExp 
       = BindingOperations.GetBindingExpression(
        targetTextBox, TextBox.TextProperty); 

      if (bndExp != null) 
      { 
       bndExp.UpdateTarget(); 
      } 
     } 
    } 

希望這會有所幫助。

+0

ohhhh我不知道2模板之間的元素之間的綁定。這是一個很好的例子!非常感謝!!!! – edelweiss

+0

在這種情況下,有沒有什麼辦法可以實現其中一個文本框作爲表格的單元格和另一個文本框的文本框? – edelweiss

+0

Plz see my **編輯2 ** –

0

我真的不知道發生了什麼事與「SelectedText」你正在試圖綁定到,但如果你正在試圖做的是在顯示「lvItems」的SelectedItem文本你的「 headText「文本框以下應該工作

<TextBox Name="headText" Text="{Binding ElementName=lvItems, Path=SelectedItem.headerObj.headervalue}" /> 

您還需要更改您的文本框」A「綁定以及。

<TextBox Name ="A" Tag="Header" Text="{Binding headerObj.headervalue}" IsReadOnly="True" BorderThickness="0" > 
</TextBox> 

假設headerObj是包類的屬性,以及headervalue就是一個屬性,headervalue是你希望綁定到值。

當SelectedItem改變時(不是單擊文本框時),「headText」中的文本將會更新。