简体   繁体   English

尝试使用 selectedChanged 在 C# 的 WPF 的文本框中显示 combobox 元素

[英]Trying to show a combobox element in a textbox in WPF of C# using selectedChanged

Here is the method:这是方法:

private void Capitales_SelectedChanged(object sender, RoutedEventArgs e)
{
    string s = Capitales.SelectedItem.ToString();
    tb.Text = "Selection: " + s;
}

I'm putting a list in the combobox, and when I compile the program, the textbox shows the next: ComboBox_MicroDocu.MainWindow+Ciudades, where "Ciudades" references my class.我在 combobox 中放了一个列表,当我编译程序时,文本框显示下一个:ComboBox_MicroDocu.MainWindow+Ciudades,其中“Ciudades”引用了我的 class。

You are writing WPF app as you whould do with Winform.您正在编写 WPF 应用程序,就像使用 Winform 一样。 This whould work, but there is a better way to do it.这确实可行,但有更好的方法来做到这一点。 Use MVVM (Model View ViewModel).使用 MVVM(模型视图 ViewModel)。 MVVM is great since it allows you to decouple your views (xaml) from your business logic (viewModels). MVVM 很棒,因为它允许您将视图 (xaml) 与业务逻辑 (viewModels) 分离。 It's also great for testability.它对可测试性也很有帮助。 Check out some good resources here https://www.tutorialspoint.com/mvvm/index.htm在这里查看一些好的资源https://www.tutorialspoint.com/mvvm/index.htm

this is how your code should looks like:这就是您的代码的样子:

MainWindow.xaml主窗口.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ComboBox Grid.Column="0" ItemsSource="{Binding Elements}" SelectedItem="{Binding SelectedElement}"/>
        <TextBox Grid.Column="1" Text="{Binding SelectedElement}"/>
    </Grid>
</Window>

MainWindow.xaml.cs主窗口.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

ViewModel.cs视图模型.cs

public class ViewModel : INotifyPropertyChanged
{
    private string _selectedElement;

    public IEnumerable<string> Elements
    {
        get
        {
            for(int i = 0; i < 10; i++)
            {
                yield return $"Element_{i}";
            }
        }
    }

    public string SelectedElement
    {
        get
        {
            return _selectedElement;
        }

        set
        {
            _selectedElement = value;
            RaisePropertyChanged();
        }
    }

    private void RaisePropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM