【发布时间】:2021-10-11 04:01:46
【问题描述】:
我遇到了 XAML 绑定问题:
我有一个带绑定的文本框,但是当我打印出绑定到文本框文本的值时,它是一个空字符串。
我尝试过的:
我尝试了不同的 UpdateSourceTriggers,并查看了 Stackoverflow。
(顺便说一句,我对 MVVM 比较陌生,这是我的第一个更大的项目)
我的 SearchView.xaml
<UserControl x:Class="MovieDatabase.MVVM.View.SearchView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MovieDatabase.MVVM.View"
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
xmlns:viewmodel="clr-namespace:MovieDatabase.MVVM.ViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
KeyUp="SearchMovie">
<UserControl.DataContext>
<viewmodel:SearchViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="600"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Top"
HorizontalAlignment="Stretch">
<TextBox Style="{StaticResource ModernSearchBar}"
Text="{Binding Path=SearchContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextChanged="TextBox_TextChanged"/>
<TextBlock Margin="10"
Text="Search for: "
Foreground="White"
FontSize="16"/>
<ComboBox Style="{StaticResource FlatCombobox}"
ItemsSource="{Binding SearchList}"
SelectedIndex="{Binding Path=SelectedSearchIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="100"/>
</StackPanel>
(我的问题与第一个文本框有关)
我的 SearchView.xaml.cs
using MovieDatabase.MVVM.ViewModel;
using System.Windows.Controls;
using System.Windows.Input;
namespace MovieDatabase.MVVM.View
{
public partial class SearchView : UserControl
{
public static SearchView Instance { get; set; }
public SearchView()
{
InitializeComponent();
DataContext = MainViewModel.SearchVM;
Instance = this;
}
private void SearchMovie(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MainViewModel.SearchVM.Search();
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource(); // Doesn't seem to help
}
}
}
我的 SearchViewModel.cs(不完整,完整的类会太多)
using MovieDatabase.Core;
using MovieDatabase.MovieSpace;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using MovieDatabase.Util;
using System.Windows.Media.Imaging;
using System;
namespace MovieDatabase.MVVM.ViewModel
{
public class SearchViewModel : ObservableObject
{
private string _searchContent;
public string SearchContent
{
get { return _searchContent; }
set
{
_searchContent = value;
OnPropertyChanged(nameof(SearchContent));
}
}
// When this method is called, SearchContent is ""
public void Search()
{
List<Movie> movies = new List<Movie>();
MessageBox.Show(SearchContent);
MainViewModel.logger.Log(Levels.INFO, $"User searching for {SearchContent} in Category {SearchList[SelectedSearchIndex]}");
if (SearchList[SelectedSearchIndex] == SearchList[0])
{
foreach (Movie movie in Movie.AllMovies)
{
if (movie.Info.title.Contains(SearchContent))
{
movies.Add(movie);
}
}
} else if (SearchList[SelectedSearchIndex] == SearchList[1])
{
foreach (Movie movie in Movie.AllMovies)
{
if (movie.Info.stars.Contains(SearchContent))
{
movies.Add(movie);
}
}
} else if (SearchList[SelectedSearchIndex] == SearchList[2])
{
foreach (Movie movie in Movie.AllMovies)
{
if (movie.Info.year == SearchContent)
{
movies.Add(movie);
}
}
} else if (SearchList[SelectedSearchIndex] == SearchList[3])
{
foreach (Movie movie in Movie.AllMovies)
{
if (movie.Info.genres.Contains(SearchContent))
{
movies.Add(movie);
}
}
}
if (movies.Count == 0)
{
return;
}
foreach (Movie mov in movies)
{
AddMovieToView(mov.Info.title);
}
}
}
}
我的可观察对象类
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace MovieDatabase.Core
{
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
【问题讨论】: