在 Windows Phone 中操作 DataContext

发布于 2024-12-28 13:13:51 字数 941 浏览 3 评论 0原文

我有一个非常小的应用程序,其中有一个笑话列表,一切正常,但每次我想在 TextBlock 中显示信息时,我都必须重新设置 DataContext。 TextBlock 在同一页面中发生变化,我只想更改笑话 ID 以获得新笑话。

 private IList<Jokes> data;
    Array variable;
    private int count;

    // Constructor
   // ObservableCollection<Jokes> = new ObservableCollectoin<Jokes>;
    public MainPage()
    {
        InitializeComponent();
        JokeRepository countryRepository = new XmlJokeRepository();
        data = countryRepository.GetCountryList().ToList<Jokes>();
        DataContext = countryRepository.GetCountryById(3);
             }
 private void nextIconClick(object sender, EventArgs e)
    {
       Random rand = new Random();
        int holder = rand.Next(1, count);
        DataContext = data.FirstOrDefault(c => c.ID == holder);
               }

在我的 XAML 中,{Binding Description} 属性可以正常工作,但我确实觉得这是多余且无用的。尽管我仍然使用相同的变量/页面,是否有另一种方法可以解决这个问题,而不必每次都重置 DataContext?

I have a really small application where I have a list of jokes, everything is working properly but I have to re-set DataContext everytime I want to display the information in the TextBlock. The TextBlock changes in the SAME page, I only want to change the joke ID to get the new joke.

 private IList<Jokes> data;
    Array variable;
    private int count;

    // Constructor
   // ObservableCollection<Jokes> = new ObservableCollectoin<Jokes>;
    public MainPage()
    {
        InitializeComponent();
        JokeRepository countryRepository = new XmlJokeRepository();
        data = countryRepository.GetCountryList().ToList<Jokes>();
        DataContext = countryRepository.GetCountryById(3);
             }
 private void nextIconClick(object sender, EventArgs e)
    {
       Random rand = new Random();
        int holder = rand.Next(1, count);
        DataContext = data.FirstOrDefault(c => c.ID == holder);
               }

Where in my XAML I have the {Binding Description} property working correctly, but I really feel this redundant and useless. Is there another way to work this without having to reset DataContext everytime although I still use the same variable/page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2025-01-04 13:13:51

您可以创建一个属性,例如 CurrentJoke,然后将您的 DataContext 设置为该属性。

然后,您可以更新该属性并触发 NotifyPropertyChanged 事件,该事件将使用 CurrentJoke 属性中的最新数据刷新 TextBox

下面是一个(未经测试但非常接近)代码示例:

public class Page : INotifyPropertyChanged
{
    private string currentJoke;
    public event PropertyChangedEventHandler PropertyChanged;

    public string CurrentJoke
    {
        get
        {
            return currentJoke;
        }
        set
        {
            if ( currentJoke == value )
                return;

            currentJoke = value;
            OnPropertyChanged( new PropertyChangedEventArgs( "CurrentJoke" ) );
        }
    }

    protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
    {
        if ( this.PropertyChanged != null )
            this.PropertyChanged( this, e );
    }
}

在 XAML 中,设置使用与 Description 相同的 {Binding CurrentJoke} 语法。如果您正确地进行了一个字段数据绑定,则整个页面的 DataContext 可能已经正确设置。

You could create a property such as CurrentJoke, then set your DataContext to that.

Then you could just update that property and fire a NotifyPropertyChanged event, which will refresh the TextBox with the latest data from your CurrentJoke property.

Here's an (untested but pretty close) code sample:

public class Page : INotifyPropertyChanged
{
    private string currentJoke;
    public event PropertyChangedEventHandler PropertyChanged;

    public string CurrentJoke
    {
        get
        {
            return currentJoke;
        }
        set
        {
            if ( currentJoke == value )
                return;

            currentJoke = value;
            OnPropertyChanged( new PropertyChangedEventArgs( "CurrentJoke" ) );
        }
    }

    protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
    {
        if ( this.PropertyChanged != null )
            this.PropertyChanged( this, e );
    }
}

In your XAML, set use the same {Binding CurrentJoke} syntax you used for the Description. The DataContext is probably already set correctly for the whole page if you have one field databinding correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文