简体   繁体   中英

ListView binding SelectedItem to Textbox

I am new to bindings and Im having trouble to get my RecipeName to be shown when I select an item in my ListView .

Im trying to bind my selected item to my TextBox . I do get my items to be displayed correctly in the ListView, but it's not previewed in my TextBox when I select an item.

What am I doing wrong here?

Xaml with the ListView and Textbox

       <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.

My class :

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));
        }
    }
}

You aren't using the ItemsSource binding to populate the listview; all that's doing is generating an error in Visual Studio's Output pane. So I've omitted it. If you bind something to ItemsSource, the "something" must be a collection of objects, not a single object like the Recipe property of your Page.

I'm guessing that when the user clicks on a Recipe in the listview, you want that Recipe to be assigned to the Recipe property of your page. Once you've got that, you can bind the TextBox's Text to a property of that Recipe.

<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"
    />

Here's another way to do it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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