WPF,数据模板中的双向绑定

货郎大叔 2019-10-24 07:37:40

<Grid>
<Button Name="button1" Content="张三">
<Button.ContentTemplate>
<DataTemplate>
<Grid>
<TextBox FontSize="13" Text="{Binding Path=." />
</Grid>
</DataTemplate>
</Button.ContentTemplate>
</Button>
<Button x:Name="button2" Content="Button" Width="75" Click="Button_Click"/>
</Grid>


private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(button1.Content.ToString());
}


在上面的Button中,数据模板中有一个TextBox,用于显示Content,当在TextBox中修改文本内容之后,单击button2,输出button1的Content,结果Content的值仍然是张三,这是为什么呢?TextBox不是双向绑定的吗,修改Text的值之后,button1的Content也应该改变啊
...全文
515 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eason0807 2019-10-29
  • 打赏
  • 举报
回复
button1当中你的数据模板上没有把content属性放到数据模板上,而是用一个textbox绑定了数据上下文,所以当你给content赋值的时候,textbox的text值是不变的
hez2010 2019-10-27
  • 打赏
  • 举报
回复
绑定默认是单向的。 双向绑定需要:{Binding Path=., Mode=TwoWay}
货郎大叔 2019-10-26
  • 打赏
  • 举报
回复
有人知道吗?
  • 打赏
  • 举报
回复
引用 8 楼 货郎大叔 的回复:
[quote=引用 7 楼 exception92 的回复:] 第一次显示的是content的值,以后的修改和Content就没有关系了,因为修改了Content模板。
我也是在文本框中修改了值啊,为什么弹出的还是张三呢?[/quote] WPF中绑定指的是以数据驱动为介质的绑定。参见#6
货郎大叔 2019-10-24
  • 打赏
  • 举报
回复
引用 7 楼 exception92 的回复:
第一次显示的是content的值,以后的修改和Content就没有关系了,因为修改了Content模板。
我也是在文本框中修改了值啊,为什么弹出的还是张三呢?
  • 打赏
  • 举报
回复
第一次显示的是content的值,以后的修改和Content就没有关系了,因为修改了Content模板。
OrdinaryCoder 2019-10-24
  • 打赏
  • 举报
回复

 <StackPanel>
        <Button Name="button1" Width="150" Height="150">
            <TextBox x:Name="text" FontSize="13" Text="{Binding Path=Name,Mode=TwoWay}" Width="50" />
        </Button>
        <Button x:Name="button2" Content="Button" Width="150" Click="Button_Click"/>
    </StackPanel>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        TestModel model;

        internal TestModel Model
        {
            get { return model; }
            set { model = value; ; NotifyPropertyChanged("Model"); }
        }
        public MainWindow()
        {
            InitializeComponent();
            Model = new TestModel();
            this.button1.DataContext = Model;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(button1.Content.ToString());
            MessageBox.Show(Model.Name);
           
        }
    }
}

Dear200892 2019-10-24
  • 打赏
  • 举报
回复
就加了一句button1.Content = "123";
货郎大叔 2019-10-24
  • 打赏
  • 举报
回复
引用 3 楼 Dear200892 的回复:
你给的源码有问题

 <Grid>
        <Button Name="button1" Content="张三">
            <Button.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBox FontSize="13" Text="{Binding Path=.}" />
                    </Grid>
                </DataTemplate>
            </Button.ContentTemplate>
        </Button>
        <Button x:Name="button2" Content="Button" Width="75" Click="Button_Click"/>
    </Grid>
少写了一个中括号,已修正
Dear200892 2019-10-24
  • 打赏
  • 举报
回复
你给的源码有问题
货郎大叔 2019-10-24
  • 打赏
  • 举报
回复
引用 1 楼 Dear200892的回复:
https://www.cnblogs.com/tianma3798/p/5765464.html INotifyPropertyChanged(向客户端发出某一属性值已更改的通知。)接口
不是你说的这个。。。
Dear200892 2019-10-24
  • 打赏
  • 举报
回复
https://www.cnblogs.com/tianma3798/p/5765464.html INotifyPropertyChanged(向客户端发出某一属性值已更改的通知。)接口
货郎大叔 2019-10-24
  • 打赏
  • 举报
回复
我说的是数据模板呢,不要修改我的代码
货郎大叔 2019-10-24
  • 打赏
  • 举报
回复
我说的是数据模板呢,不要修改我的代码
OrdinaryCoder 2019-10-24
  • 打赏
  • 举报
回复
我6#的代码你可以试一下 直接在button里嵌套Textbox
货郎大叔 2019-10-24
  • 打赏
  • 举报
回复
引用 9 楼 exception92的回复:
[quote=引用 8 楼 货郎大叔 的回复:] [quote=引用 7 楼 exception92 的回复:] 第一次显示的是content的值,以后的修改和Content就没有关系了,因为修改了Content模板。
我也是在文本框中修改了值啊,为什么弹出的还是张三呢?[/quote] WPF中绑定指的是以数据驱动为介质的绑定。参见#6[/quote] 怎么说得不沾边啊。。

111,094

社区成员

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

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

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