2012-09-23 44 views
0

我有一個文本框將保留最後輸入的10個條目,類似於Internet Explorer中的搜索框。用戶可以點擊下拉菜單查看最後10個條目。下拉菜單是一個組合框。我創建了一個綁定到組合框的字符串的Observable集合Itemssource.Below是代碼。存儲組合框的文本條目

的XAML

<Grid x:Name="TextBox_grid" Margin="0,0,40,0" Width="360" Height="23"> 
    <ComboBox Name="cb" Margin="0,0,-29,0" Style="{DynamicResource Onyx_Combo}" ItemsSource="{Binding TextEntries, ElementName=TheMainWindow, Mode=OneWay}" IsEditable="False" Visibility="Visible" /> 
    <Rectangle Fill="#FF131210" Stroke="Black" RadiusX="2" RadiusY="2"/> 
    <TextBox Name=UniversalTextBox Margin="0" Background="{x:Null}" BorderBrush="{x:Null}" FontSize="16" Foreground="#FFA0A0A0" TextWrapping="Wrap" PreviewKeyDown="TextBox_PreviewKeyDown"/> 
</Grid> 

代碼

public partial class Window1 : Window 
{ 

    private ObservableCollection<string> m_TextEntries = new ObservableCollection<string>(); 

    public Window1() 
    { 
     InitializeComponent(); 
    } 
    public ObservableCollection<string> TextEntries 
    { 
     get { return m_TextEntries; } 
    } 
    private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 
     if (textBox == null) 
      return; 

     if (e.Key == Key.Enter) 
     { 
      PopulateHistoryList(textBox.Text); 
      e.Handled = true; 
     } 
     if (e.Key == Key.Escape) 
     { 
      e.Handled = true; 
     } 
    } 

    private void PopulateHistoryList(string text) 
    { 
     m_TextEntries.Add(text); 
    } 

    private event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 

上面的代碼將填充TextEntries集合時,按鍵上的文本框中輸入。我需要兩件東西

  1. 如何設置組合框的選定項目,以及如何將其綁定到我的文本框。
  2. 組合框(dropmenu)應該只顯示下拉菜單中的最後10個條目。

由於提前,

回答

0

使用Expesssion混合,結合一個控制到另一個控制的屬性值的屬性的值是容易的,它被稱爲ElementProperty結合,這裏是一個屏幕截圖在Blend中創建此功能的位置,請注意文本框是「對象和時間軸」面板中的選定元素,它是屬性面板中Text屬性右側的「小框」,它已被點擊以生成上下文菜單如圖所示... enter image description here

一旦你選擇'元素屬性綁定'爲您的文本框的文本屬性,您的光標將變成一個小靶心圖標,您現在將使用它來指示要綁定到的內容,方法是在設計畫布或對象和時間軸面板中單擊它,同時光標以這種方式出現... enter image description here 這裏我們看到組合框的'SelectedValue'屬性被選爲文本框中顯示內容的來源。一旦完成,文本框將自動立即設置爲顯示組合中選擇的內容。當你這樣做時,一定要看看Blend在XAML中做了什麼,因爲它可以幫助你更好地理解實際正在發生的事情,甚至可以教你一些關於XAML綁定語法的東西。

至於只有最後十個條目的列表......有幾種方法可以做到這一點,每個方法或多或少都適合,取決於周圍的環境,但這裏有一種方法;只需運行與此類似,每當有框項添加到它的程序:

// assuming 'listItems' is your ObservableCollection 
string[] items = listItems.ToArray(); 

// prepare a new array for the current ten 
string[] tenItems = new string[10]; 

// copy a subset of length ten, to the temp array, the set your ObservableCollection to this array. 
Array.Copy(items, (items.Length - 10), tenItems, 0, 10); 

注意:磁盤陣列.Copy假設項目被添加到該觀察集合的唯一途徑就是通過某種形式。新增的,它總是將它們添加到列表的末尾...

0

部分答案

<TextBlock Text="{Binding ElementName=cb, Path=SelectedValue}" /> 


<ComboBox x:Name="cb" ItemsSource="{Binding Path=Fields}" SelectedValue="{Binding Path=SelectedValue}" /> 

如果設置窗口的datacontext

DataContext="{Binding RelativeSource={RelativeSource self}}">