Why DataBinding is not working?
<TextBox Text="{Binding Path=local:MainWindow.SearchPlayer,
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
this is my class:
public partial class MainWindow : Window
{
private Store store = new Store();
private string _searchPlayer;
public string SearchPlayer
{
get
{
return _searchPlayer;
}
set
{
_searchPlayer = value;
if(_searchPlayer!="")
{
ACT.DataContext = store.SearchedPlayers
.Where(x => x.StartsWith(_searchPlayer)).ToList();
}
else
{
ACT.DataContext = store.Last10SearchedPlayers;
}
}
}
public MainWindow()
{...............}
I set breakpoint on SearchPlayer setter but it’s never worked.
I don’t think
Binding Path=local:MainWindow.SearchPlayerwill work because MainWindow is a class, not an instance. It might work if SearchPlayer was static, but I don’t think you want that.Just use
Binding Path=SearchPlayerand make sure the DataContext is set correctly. In the MainWindow’s constructor:this.DataContext=this;(vary depending on where the textbox is).And note that MainWindow should implement the INotifyProperty interface, and the Setter of SearchPlayer should call OnPropertyChanged.