c# - WPF 级联组合框到文本框

标签 c# wpf

我是编程新手,在将 ComboBox 级联到 TextBox 时遇到了一些困难。雇用是我的代码:

主窗口.xaml

Window x:Class="WpfApplication26.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,0,-1">
    <ComboBox x:Name="Category" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" SelectionChanged="Category_SelectionChanged" Margin="309,52,0,0">
        <ListBoxItem Name="clComboBoxItem1" Content="Comedy" Height="25"/>
        <ListBoxItem Name="clComboBoxItem2" Content="Drama" Height="25"/>
        <ListBoxItem Name="clComboBoxItem3" Content="Science Fisction" Height="25"/>
    </ComboBox>
    <ComboBox x:Name="Shows" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="309,79,0,0" SelectionChanged="Shows_SelectionChanged" />
    <TextBox x:Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="309,106,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

</Grid>

主窗口.xamlcs

namespace WpfApplication26
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();



    }



    private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {


        //Create List for Comedy selection
        List<fruits> dropDownList_Comedy = new List<fruits>();
        dropDownList_Comedy.Add(new fruits() { price = 10, Name = "Apples" });
        dropDownList_Comedy.Add(new fruits() { price = 9, Name = "Bammamas" });
        dropDownList_Comedy.Add(new fruits() { price = 1, Name = "Mango" });
        //Create List for Drama selection
        List<fruits> dropDownList_Drama = new List<fruits>();

        dropDownList_Drama.Add(new fruits() { price = 10, Name = "Liver"});

        //Create List for Science Fiction selection
        List<fruits> dropDownList_SciFi = new List<fruits>();

        dropDownList_SciFi.Add(new fruits() { price = 10, Name = "Apples2" });

        //Check for SelectedIndex value and assign appropriate list to 2nd
        if (Category.SelectedIndex == 0)
        {
            Shows.ItemsSource = dropDownList_Comedy;
            Shows.DisplayMemberPath = "Name";


        }
        else if (Category.SelectedIndex == 1)
        {
            Shows.ItemsSource = dropDownList_Drama;
            Shows.DisplayMemberPath = "Name";

        }
        else if (Category.SelectedIndex == 2)
        {
            Shows.ItemsSource = dropDownList_SciFi;
            Shows.DisplayMemberPath = "Name";
        }
        {

        }
    }

    private void Shows_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        {
            fruits dropDownList_Comedy = Shows.SelectedItem as fruits ;
            TextBox1.Text = Convert.ToString(dropDownList_Comedy.price);

        }

        {
            fruits dropDownList_Drama = Shows.SelectedItem as fruits;
            TextBox1.Text = Convert.ToString(dropDownList_Drama.price);
        }

        {
            fruits dropDownList_SciFi = Shows.SelectedItem as fruits;
            TextBox1.Text = Convert.ToString(dropDownList_SciFi.price);
        }
    }           
}

Class1.cs

class fruits
{
    public int price { get; set; }
    public string Name { get; set; }
}

System.NullReferenceException 有问题。我不知道该怎么办。感谢您的帮助。

最佳答案

您在 Shows_SelectionChanged 函数的第二个 block 中使用 SelectedValue 而不是 SelectedItem。

SelectedValue 返回一个字符串,它不能被解析成一个水果(所以它会返回 null)。并且您在使用 null.price 时试图访问 null 的成员,因此它会引发 NullReferenceException。

编辑

如果您只想在 TextBlock 中显示 ListBox 的 SelectedItem,您可以这样做:

<ListBox x:Name="MyListBox">
    ...
</ListBox>
<TextBox Text="{Binding ElementName=MyListBox, 
                        Path=SelectedItem, 
                        TargetNullValue=Nothing is selected, 
                        Mode=OneWay}"/>

在这种情况下您不需要代码隐藏。

关于c# - WPF 级联组合框到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610798/

相关文章:

c# - C# 中的正则表达式 - 如何仅替换 Match 中的一个特定组?

c# - 如何在 C# 中声明项目中所有类都应该可以访问的公共(public)变量?

c# - 如何阻止多次鼠标单击下一个 Windows WPF 中的元素?

c# - 在 WPF 应用程序中的浏览器中打开 HTML 文件

c# - 通过Graph SDK获取用户联系人

c# - 可以使用 Xamarin 开发 Linux 应用程序吗?

c# - 由于 DPI 意识,Microsoft Store 认证失败

c# - 如何级联新窗口打开的位置?

c# - 使用 C# 将 Config 部分绑定(bind)到 DataTable

c# - 如何使用 PropertyChangedCallBack