开发者

Why DataBinding is not working?

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

        publi开发者_高级运维c MainWindow()
        {...............}

I set breakpoint on SearchPlayer setter but it's never worked.


<TextBox Text="{Binding Path=SearchPlayer,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}
                        Mode=TwoWay,
                        UpdateSourceTrigger=PropertyChanged}"  />


I don't think Binding Path=local:MainWindow.SearchPlayer will 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=SearchPlayer and 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜