WPF - DataBind TextBlock.Text 到整数

发布于 2024-10-05 05:40:24 字数 379 浏览 2 评论 0原文

我知道这是一个非常简单的问题..

我有一个 Textblock,它的文本属性我想将数据绑定到代码隐藏中的一个整数..现在我正在这样做

<TextBlock Name="TextBlockCompeltedSongsNumber" Text="{Binding}"></TextBlock>

然后在 c# 中..

            this.TextBlockCompeltedSongsNumber.DataContext = CompeltedTracks;

其中 CompletedTracks 是一个 public int

什么我失踪了吗?

I know this a really simple question..

I have a Textblock who's text property I want to be databound to an integer I have in the codebehind.. right now I'm doing this

<TextBlock Name="TextBlockCompeltedSongsNumber" Text="{Binding}"></TextBlock>

Then in c#..

            this.TextBlockCompeltedSongsNumber.DataContext = CompeltedTracks;

Where CompletedTracks is a public int

What am I missing?

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

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

发布评论

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

评论(2

记忆消瘦 2024-10-12 05:40:24

最后,我刚刚创建了一个

    //Global Declaration
information info = new information();

稍后实现 INotifyPorpertyChange 的类...

            this.TextBlockCompeltedSongsNumber.DataContext = info;

以及信息类

public class information : INotifyPropertyChanged
{
    private int failedTracks = 0;
    public int FailedTracks { get { return failedTracks; } set { failedTracks = value; OnPropertyChanged("FailedTracks"); } }
    private int compeltedTracks = 0;
    public int CompeltedTracks { get { return compeltedTracks; } set { compeltedTracks = value; OnPropertyChanged("CompeltedTracks"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

,最后在 xaml 中

                    <TextBlock Name="TextBlockCompletedSongs" Margin="5,0,5,0">Completed Songs:</TextBlock>
                <TextBlock Name="TextBlockCompeltedSongsNumber" Text="{Binding Path=CompeltedTracks}"></TextBlock>
                <TextBlock Name="TextBlockFailedSongs" Margin="5,0,5,0">Failed Songs:</TextBlock>
                <TextBlock Name="TextBlockFailedSongsNumber" Text="{Binding Path=FailedTracks}"></TextBlock>

对于应该很简单的东西来说似乎需要做很多工作...但我无法让它以任何其他方式工作...我不知道我做错了什么:O

In the end I just made a class that implemented INotifyPorpertyChange

    //Global Declaration
information info = new information();

later...

            this.TextBlockCompeltedSongsNumber.DataContext = info;

and the information class

public class information : INotifyPropertyChanged
{
    private int failedTracks = 0;
    public int FailedTracks { get { return failedTracks; } set { failedTracks = value; OnPropertyChanged("FailedTracks"); } }
    private int compeltedTracks = 0;
    public int CompeltedTracks { get { return compeltedTracks; } set { compeltedTracks = value; OnPropertyChanged("CompeltedTracks"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

and finally in the xaml

                    <TextBlock Name="TextBlockCompletedSongs" Margin="5,0,5,0">Completed Songs:</TextBlock>
                <TextBlock Name="TextBlockCompeltedSongsNumber" Text="{Binding Path=CompeltedTracks}"></TextBlock>
                <TextBlock Name="TextBlockFailedSongs" Margin="5,0,5,0">Failed Songs:</TextBlock>
                <TextBlock Name="TextBlockFailedSongsNumber" Text="{Binding Path=FailedTracks}"></TextBlock>

It seems like a lot of work for something that should be simple... but I couldnt get it to work any other way... I dunno what I was doing wrong :O

假面具 2024-10-12 05:40:24

也许你设置 DataContext 太晚了。渲染完之后。

直接绑定到属性并引发 PropertyChanged 事件不是更好吗?

<TextBlock Text="{Binding CompletedTracks}"></TextBlock>

并将 TextBlock 父级的 DataContext 设置为 CompletedTracks 的所有者。

Maybe you set DataContext too late. After it's rendered.

Wouldn't it better to bind directly to a property and raise PropertyChanged event?

<TextBlock Text="{Binding CompletedTracks}"></TextBlock>

and set DataContext of the TextBlock's parent to CompletedTracks's owner.

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