Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将组合框绑定到文件的值_C#_Wpf - Fatal编程技术网

C# 将组合框绑定到文件的值

C# 将组合框绑定到文件的值,c#,wpf,C#,Wpf,我有一个组合框,可以在其中键入数据库服务器名称。我只想让它记住我到目前为止键入的内容,下次它将其添加到其中的项目列表中时,这样我就不必在每次运行应用程序时再次键入相同的数据库服务器名称。 至于保存我在combox中键入的名称,我可以将它们保存在一个文件中,比如文本文件、Json、XML等等 我不知道怎么装订?什么时候加载文件?你能帮我举个例子吗 <ComboBox x:Name="serverTxt" Height="23" VerticalAlignment="Top" Text="{

我有一个组合框,可以在其中键入数据库服务器名称。我只想让它记住我到目前为止键入的内容,下次它将其添加到其中的项目列表中时,这样我就不必在每次运行应用程序时再次键入相同的数据库服务器名称。 至于保存我在combox中键入的名称,我可以将它们保存在一个文件中,比如文本文件、Json、XML等等

我不知道怎么装订?什么时候加载文件?你能帮我举个例子吗

<ComboBox x:Name="serverTxt" Height="23"  VerticalAlignment="Top" Text="{Binding Path=ServerNames}"/>

以下是一些代码,经过一些更新,添加了存储/检索功能。这至少能让你开始。请注意,此解决方案需要在窗口中添加第二个元素(我在此处添加了第二个组合框),因为它会在
LostFocus
时触发,否则它将在键入时为每个字符更新

按如下方式设置xaml:

<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="149,43,0,0" VerticalAlignment="Top" Width="120" IsEditable="True"
          ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" Text="{Binding NewItem, UpdateSourceTrigger=LostFocus}"/>
<ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="349,122,0,0" VerticalAlignment="Top" Width="120"/>

然后打开主窗口:

public partial class MainWindow : Window
{
    private string _selectedItem;

    private ObservableCollection<string> ServerNames;
    private string fileLocation = @"C:\Temp\ServerNames.txt";

    public MainWindow()
    {
       ServerNames = new ObservableCollection<string>();

        if (File.Exists(fileLocation))
        {
            var list = File.ReadAllLines(fileLocation).ToList();
            list.ForEach(ServerNames.Add);
        }
        DataContext = this;
        InitializeComponent();
    }

    public IEnumerable Items => ServerNames;

    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }

    public string NewItem
    {
        set
        {
            if (SelectedItem != null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(value))
            {
                ServerNames.Add(value);
                SelectedItem = value;
            }
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        if (!File.Exists(fileLocation))
        {
            File.Create(fileLocation);
        }

        File.WriteAllLines(fileLocation, ServerNames);
    }
}
公共部分类主窗口:窗口
{
私有字符串_selectedItem;
私有可观察收集服务器名称;
私有字符串fileLocation=@“C:\Temp\ServerNames.txt”;
公共主窗口()
{
ServerNames=新的ObservableCollection();
if(File.Exists(fileLocation))
{
var list=File.ReadAllLines(fileLocation.ToList();
list.ForEach(ServerNames.Add);
}
DataContext=this;
初始化组件();
}
public IEnumerable Items=>servername;
公共字符串SelectedItem
{
获取{return\u selectedItem;}
设置
{
_选择editem=值;
OnPropertyChanged(“SelectedItem”);
}
}
公共字符串NewItem
{
设置
{
如果(SelectedItem!=null)
{
返回;
}
如果(!string.IsNullOrEmpty(值))
{
ServerNames.Add(value);
选择editem=值;
}
}
}
受保护的无效OnPropertyChanged(字符串propertyName)
{
var handler=this.PropertyChanged;
handler?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
公共事件属性更改事件处理程序属性更改;
私有无效窗口\u关闭(对象发送方,取消事件参数)
{
如果(!File.Exists(fileLocation))
{
创建(fileLocation);
}
writeAllines(文件位置、服务器名称);
}
}
以下是一些代码,经过一些更新,添加了存储/检索功能。这至少能让你开始。请注意,此解决方案需要在窗口中添加第二个元素(我在此处添加了第二个组合框),因为它会在
LostFocus
时触发,否则它将在键入时为每个字符更新

按如下方式设置xaml:

<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="149,43,0,0" VerticalAlignment="Top" Width="120" IsEditable="True"
          ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" Text="{Binding NewItem, UpdateSourceTrigger=LostFocus}"/>
<ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="349,122,0,0" VerticalAlignment="Top" Width="120"/>

然后打开主窗口:

public partial class MainWindow : Window
{
    private string _selectedItem;

    private ObservableCollection<string> ServerNames;
    private string fileLocation = @"C:\Temp\ServerNames.txt";

    public MainWindow()
    {
       ServerNames = new ObservableCollection<string>();

        if (File.Exists(fileLocation))
        {
            var list = File.ReadAllLines(fileLocation).ToList();
            list.ForEach(ServerNames.Add);
        }
        DataContext = this;
        InitializeComponent();
    }

    public IEnumerable Items => ServerNames;

    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }

    public string NewItem
    {
        set
        {
            if (SelectedItem != null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(value))
            {
                ServerNames.Add(value);
                SelectedItem = value;
            }
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        if (!File.Exists(fileLocation))
        {
            File.Create(fileLocation);
        }

        File.WriteAllLines(fileLocation, ServerNames);
    }
}
公共部分类主窗口:窗口
{
私有字符串_selectedItem;
私有可观察收集服务器名称;
私有字符串fileLocation=@“C:\Temp\ServerNames.txt”;
公共主窗口()
{
ServerNames=新的ObservableCollection();
if(File.Exists(fileLocation))
{
var list=File.ReadAllLines(fileLocation.ToList();
list.ForEach(ServerNames.Add);
}
DataContext=this;
初始化组件();
}
public IEnumerable Items=>servername;
公共字符串SelectedItem
{
获取{return\u selectedItem;}
设置
{
_选择editem=值;
OnPropertyChanged(“SelectedItem”);
}
}
公共字符串NewItem
{
设置
{
如果(SelectedItem!=null)
{
返回;
}
如果(!string.IsNullOrEmpty(值))
{
ServerNames.Add(value);
选择editem=值;
}
}
}
受保护的无效OnPropertyChanged(字符串propertyName)
{
var handler=this.PropertyChanged;
handler?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
公共事件属性更改事件处理程序属性更改;
私有无效窗口\u关闭(对象发送方,取消事件参数)
{
如果(!File.Exists(fileLocation))
{
创建(fileLocation);
}
writeAllines(文件位置、服务器名称);
}
}

你是说自动完成吗?或者您正在添加新项目并希望保存它们?您是否熟悉使用
属性.设置
?@DrewJordan仅加载我从该文件添加的项目。谢谢。你是说自动完成吗?或者您正在添加新项目并希望保存它们?您是否熟悉使用
属性.设置
?@DrewJordan仅加载我从该文件添加的项目。谢谢