WPF怎么实现数据和控件的绑定

公西雒 2013-08-13 02:26:37
例如textbox,怎么让他随着我定义的string型变量的改变而显示不同的字符?
...全文
324 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
公西雒 2013-08-14
  • 打赏
  • 举报
回复
引用 7 楼 KumaPower 的回复:
[quote=引用 6 楼 danding_ge 的回复:] 用Image。。。。
一样的啊。假设项目文件结构如下:
<Window x:Class="WpfTest.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300">
    <Grid>
        <Image Height="150" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="{Binding Path=MyString}" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,176,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;

namespace WpfTest
{
    /// <summary>
    /// Window2.xaml 的交互逻辑
    /// </summary>
    public partial class Window2 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private String m_mystring = "/Images/Bluehills.jpg";
        public String MyString
        {
            get { return m_mystring; }
            set { 
                m_mystring = value; 
                NotifyPropertyChanged("MyString");
            }
        }

        public Window2()
        {
            InitializeComponent();

            this.DataContext = this;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MyString = "/Images/Sunset.jpg";
        }
    }
}
[/quote] 哦哦 谢谢谢谢! 有空再帮我看看这个吧http://bbs.csdn.net/topics/390547382
Bonjour-你好 2013-08-14
  • 打赏
  • 举报
回复
引用 6 楼 danding_ge 的回复:
用Image。。。。


一样的啊。假设项目文件结构如下:

<Window x:Class="WpfTest.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
<Image Height="150" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="{Binding Path=MyString}" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,176,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>


using System;
using System.ComponentModel;
using System.Windows;

namespace WpfTest
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

private String m_mystring = "/Images/Bluehills.jpg";
public String MyString
{
get { return m_mystring; }
set {
m_mystring = value;
NotifyPropertyChanged("MyString");
}
}

public Window2()
{
InitializeComponent();

this.DataContext = this;
}

private void button1_Click(object sender, RoutedEventArgs e)
{
MyString = "/Images/Sunset.jpg";
}
}
}
公西雒 2013-08-14
  • 打赏
  • 举报
回复
引用 5 楼 KumaPower 的回复:
[quote=引用 4 楼 danding_ge 的回复:] 如果换成图片,Text="{Binding Path=MyString}"怎么改呢?
TextBox要绑定图片、要显示图片?![/quote] 用Image。。。。
Bonjour-你好 2013-08-14
  • 打赏
  • 举报
回复
引用 4 楼 danding_ge 的回复:
如果换成图片,Text="{Binding Path=MyString}"怎么改呢?
TextBox要绑定图片、要显示图片?!
公西雒 2013-08-14
  • 打赏
  • 举报
回复
引用 3 楼 KumaPower 的回复:
[quote=引用 2 楼 danding_ge 的回复:] 你还是回答了吧。。。。我很急!
<Window x:Class="WpfTest.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300">
    <Grid>
        <TextBox Width="100" VerticalAlignment="Center" Text="{Binding Path=MyString}"/>
    </Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;

namespace WpfTest
{
    /// <summary>
    /// Window2.xaml 的交互逻辑
    /// </summary>
    public partial class Window2 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private String m_mystring = "0";
        public String MyString
        {
            get { return m_mystring; }
            set { 
                m_mystring = value; 
                NotifyPropertyChanged("MyString");
            }
        }

        public Window2()
        {
            InitializeComponent();

            MyString = "1";
            this.DataContext = this;
        }
    }
}
[/quote] 如果换成图片,Text="{Binding Path=MyString}"怎么改呢?
Bonjour-你好 2013-08-13
  • 打赏
  • 举报
回复
引用 2 楼 danding_ge 的回复:
你还是回答了吧。。。。我很急!
<Window x:Class="WpfTest.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300">
    <Grid>
        <TextBox Width="100" VerticalAlignment="Center" Text="{Binding Path=MyString}"/>
    </Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;

namespace WpfTest
{
    /// <summary>
    /// Window2.xaml 的交互逻辑
    /// </summary>
    public partial class Window2 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private String m_mystring = "0";
        public String MyString
        {
            get { return m_mystring; }
            set { 
                m_mystring = value; 
                NotifyPropertyChanged("MyString");
            }
        }

        public Window2()
        {
            InitializeComponent();

            MyString = "1";
            this.DataContext = this;
        }
    }
}
公西雒 2013-08-13
  • 打赏
  • 举报
回复
引用 1 楼 KumaPower 的回复:
鉴于楼主的这个帖http://bbs.csdn.net/topics/390541523,我还是先不要回答好了
你还是回答了吧。。。。我很急!
Bonjour-你好 2013-08-13
  • 打赏
  • 举报
回复
鉴于楼主的这个帖http://bbs.csdn.net/topics/390541523,我还是先不要回答好了

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧