绑定到对象属性 [英] binding to an object properties

查看:98
本文介绍了绑定到对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案是将article对象的WordsNum属性绑定到文本块,以便在属性更改时更新它。我花了好几个小时来调试它,但这似乎是一个丑陋的错误!

 <   Window     x:Class   =  Article_Word_Counter.MainWindow  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

标题 = MainWindow 高度 = 350 宽度 = 525 FontSize = 14 >
< 网格 >
< StackPanel >
< ; 标签 > 选择您的文章: < / Label >
< TextBox 名称 = txtFilePath 高度 = 30 > < / TextBox >
< 按钮 名称 = btnBrowse 高度 = 30 宽度 = 150 点击 = btnBrowse_Click > 浏览< /按钮 >
< GroupBox 标题 = 文章信息 >
< StackPanel >
< StackPanel 方向 = 水平 >
< 标签 > 字数:< / Label >
< TextBlock 文本 = {Binding Path = WordsNum,UpdateSourceTrigger = PropertyChanged} > < / TextBlock >
< / StackPanel >

< / StackPanel >
< / GroupBox >
< / StackPanel >
< / Grid >
< / Window >





< pre lang =c#> 命名空间 Article_Word_Cou nter
{
public partial class MainWindow:Window
{
public 文章CurrentArticle { get ; set ; }
public MainWindow()
{
InitializeComponent();
base .DataContext = CurrentArticle;
}

private void btnBrowse_Click( object sender,RoutedEventArgs e)
{
// openfiledialog的逻辑
// strFilePath是从逻辑上获得的
CurrentArticle = new Article(strFilePath);
}
}
public class 文章:INotifyPropertyChanged
{
private int wordsNum;
public int WordsNum
{
获取
{
return wordsNum;
}
set
{
wordsNum = value ;
this .OnPropertyChanged( WordsNum< /跨度>);
}
}

#region ctors
// public
public 文章( string strFilePath)
{
// 计算逻辑intCount
WordsNum = intCount;
}

#endregion

#region INotifyPropertyChanged成员

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged( string propName)
{
if this .PropertyChanged!= null
this .PropertyChanged( this PropertyChangedEventArgs(propName));
}

#endregion
}
}

解决方案

btnBrowse_Click 中设置 CurrentArticle DataContext 仍保留旧值...



您可以设置 DataContext

  private   void  btnBrowse_Click( object  sender,RoutedEventArgs e)
{
// openfiledialog的逻辑
// strFilePath是从上面的逻辑中获得的
CurrentArticle = new Article(strFilePath);
DataContext = CurrentArticle;
}


The scenario is to bind the WordsNum property of article object to a textblock so that it is updated when the property changes. I have spent hours on debugging this but it seems to be an ugly error!

<Window x:Class="Article_Word_Counter.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525" FontSize="14">
    <Grid>
        <StackPanel>
            <Label>Choose your article:</Label>
            <TextBox Name="txtFilePath" Height="30"></TextBox>
            <Button Name="btnBrowse" Height="30" Width="150" Click="btnBrowse_Click">Browse</Button>
            <GroupBox Header="Article Info"> 
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label>Numder of Words: </Label>
                        <TextBlock Text="{Binding Path=WordsNum, UpdateSourceTrigger=PropertyChanged}"></TextBlock> 
                    </StackPanel>
                    
                </StackPanel>
            </GroupBox>
        </StackPanel>
    </Grid>
</Window>



namespace Article_Word_Counter
{
    public partial class MainWindow : Window
    {
        public Article CurrentArticle { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            base.DataContext = CurrentArticle;  
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            //logic for openfiledialog
            //strFilePath is obtained from above logic
            CurrentArticle = new Article(strFilePath);
        }
    }
    public class Article: INotifyPropertyChanged
    {
        private int wordsNum;
        public int WordsNum 
        {
            get
            {
                return wordsNum;
            }
            set
            {
                wordsNum = value;
                this.OnPropertyChanged("WordsNum");
            }
        }
        
        #region ctors
        //public 
        public Article(string strFilePath)
        {
            //logic for calculatin intCount 
            WordsNum = intCount;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        #endregion
    }
}

解决方案

In btnBrowse_Click you set the value of CurrentArticle but the DataContext still keeps the old value...


You can set the DataContext too:

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
    //logic for openfiledialog
    //strFilePath is obtained from above logic
    CurrentArticle = new Article(strFilePath);
    DataContext = CurrentArticle;
}


这篇关于绑定到对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆