存储组合框的文本条目

时间:2012-09-23 21:10:38

标签: c# wpf xaml wpf-controls

我有一个文本框,将保留最后输入的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));
    }

当在文本框上按下Enter键时,上面的代码将填充TextEntries集合。我需要两件事

  1. 如何设置组合框的Selected Item以及如何将其绑定到我的文本框。
  2. 组合框(dropmenu)应该只显示下拉菜单中的最后10个条目。
  3. 提前致谢,

2 个答案:

答案 0 :(得分:0)

使用Expesssion Blend,将一个控件的属性值绑定到另一个控件的属性值很容易,它被称为ElementProperty Binding,这里是一个截图,你可以在其中访问在Blend中创建它的能力,请注意,“文本框”是“对象和时间轴”面板中的选定元素,它是属性面板中“文本”属性右侧的“小框”,已单击该框以生成上下文菜单... 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);

注意:Array .Copy假定项目被添加到observable集合的唯一方法是通过某种形式的.Add,它总是将它们添加到列表的末尾...

答案 1 :(得分: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}}">