本文介绍了数据结合的方法的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示的方法在WPF 文本框的输出。我只是想要一个简单的尝试,打印在文本框

I'm trying to display the output of a method in a WPF TextBox. I'm just trying a simple attempt, to print a single string 3 in a TextBox.

我试图做到这一点通过以下方式,使用的ObjectDataProvider

I'm trying to do it the following way, using an ObjectDataProvider:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <ObjectDataProvider x:Key="dataprovider" ObjectType="{x:Type system:String}" MethodName="GetValue">

        </ObjectDataProvider>
    </Window.Resources>
    <Grid>

        <TextBox Text="{Binding Source={StaticResource dataprovider}, Mode=OneWay}" HorizontalAlignment="Left" Height="23" Margin="201,168,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="120"/>

    </Grid>
</Window>

和这里是我后面的代码:

And here's my code behind:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public string GetValue()
        {
            return "3";
        }
    }
}



我越来越没有输出。在文本框仅仅是空白。我要去哪里错了?

I'm getting no output. the TextBox is just blank. Where am I going wrong?

推荐答案

而不是的ObjectDataProvider 的创建属性像这样的:

Instead of ObjectDataProvider create a property like this:

public string GetMethod
{
     get
     {
         return GetValue();
     }
}

public MainWindow()
{
     InitializeComponent();
     DataContext = this;
}

在XAML中删除的ObjectDataProvider 和公正的:

Then in the XAML remove ObjectDataProvider and just:

<TextBox Text="{Binding GetMethod, Mode=OneWay}" HorizontalAlignment="Left" 
         Height="23" Margin="201,168,0,0" TextWrapping="Wrap" 
         VerticalAlignment="Top" Width="120"/>

这篇关于数据结合的方法的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:17