【问题标题】:ListView binding SelectedItem to TextboxListView 将 SelectedItem 绑定到文本框
【发布时间】:2019-08-21 17:27:00
【问题描述】:

我是绑定新手,当我在 ListView 中选择一个项目时,我无法显示我的 RecipeName。

我正在尝试将我选择的项目绑定到我的TextBox。我确实让我的项目在 ListView 中正确显示,但是当我选择一个项目时它没有在我的 TextBox 中预览。

我在这里做错了什么?

带有 ListView 和文本框的 Xaml

       <ListView Grid.Column="0" 
                  Name="listOfRecipes" 
                  Margin="10,10,10,240" 
                  Height="150"
                  ItemsSource="{Binding Path=Recipe}" 
                  SelectedItem="{Binding Path=RecipeName, Mode=TwoWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=RecipeName, Mode=TwoWay}"  /> // This lists my items in the ListView Correctly
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <TextBox Text="{Binding Path=RecipeName, Mode=TwoWay}" Grid.Column="1" Height="50" Margin="10,10,-10,340"/> // This doesn't preview the selected item.

我的班级:

public partial class ViewAll : Page, INotifyPropertyChanged
    {
        private Recipe _recipe;
        public Recipe Recipe
        {
            get { return _recipe; }
            set
            {
                if(_recipe != value)
                {
                    _recipe = value;
                    OnPropertyChanged("Recipe");
                }
            }
        }

        public ViewAll()
        {
            InitializeComponent();
            LoadItemTemplate();
        }

        public void LoadItemTemplate()
        {
            mrydendbEntities dbe = new mrydendbEntities();
            listOfRecipes.ItemsSource = dbe.Recipe.ToList();
            listOfRecipes.SelectedItem = dbe.Recipe.First();

        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    您没有使用 ItemsSource 绑定来填充列表视图;所做的只是在 Visual Studio 的“输出”窗格中生成错误。所以我省略了它。如果将某些内容绑定到 ItemsSource,则“某些内容”必须是对象的集合,而不是像页面的 Recipe 属性那样的单个对象。

    我猜当用户点击列表视图中的Recipe 时,您希望将Recipe 分配给页面的Recipe 属性。完成后,您可以将 TextBox 的文本绑定到该配方的属性。

    <ListView Grid.Column="0" 
        Name="listOfRecipes" 
        Margin="10,10,10,240" 
        Height="150"
        SelectedItem="{Binding Recipe, RelativeSource={RelativeSource AncestorType=Page}}
        >
    
    <!-- snip -->
    
    <TextBox 
        Text="{Binding Recipe.Name, RelativeSource={RelativeSource AncestorType=Page}}" 
        Grid.Column="1" 
        Height="50" 
        Margin="10,10,-10,340"
        />
    

    这是另一种方法。

    <TextBox 
        Text="{Binding SelectedItem.Name, ElementName=listOfRecipes}" 
        Grid.Column="1" 
        Height="50" 
        Margin="10,10,-10,340"
        />
    

    【讨论】:

    • 哇,你不是每天都能在一个回复中得到两个有效的解决方案。它完美地工作,完全符合我的要求。我还学到了有关“输出”窗格的新知识。不知道您会在那里看到绑定问题!非常感谢。 :)
    • @Mlol 很高兴为您提供帮助!您可以通过在绑定中添加PresentationTraceSources.TraceLevel=High 来获得极其详细的调试信息。
    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多