您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> wpf 自定义控件绑定问题

wpf 自定义控件绑定问题

来源:网络整理     时间:2016/5/19 2:00:28     关键词:自定义,wpf

关于网友提出的“wpf 自定义控件绑定问题”问题疑问,本网通过在网上对“wpf 自定义控件绑定问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:wpf 自定义控件绑定问题
描述:

请教各位,我自定义了一个控件,如下,
xaml:
<>
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    
        
    

代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
namespace WpfApplication1
{
    /// 


    /// WebB.xaml 的交互逻辑
    /// 

    public partial class WebB : UserControl
    {
        public WebB()
        {
            InitializeComponent();
        }
        public static readonly DependencyProperty ConProperty = DependencyProperty.Register("Con",typeof(string),typeof(WebB), new PropertyMetadata("")) ;
       
        public string Con
        {
            set{ 
                SetValue(ConProperty,value) ;
                wb.NavigateToString(value) ;
            }
            get{
                return (string)GetValue(ConProperty);
                
            }
        }
    }
}
主窗体:
xaml : 
<>
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:grg="clr-namespace:WpfApplication1"
        Title="Window2" Height="300" Width="300" >
    
        
            
            
        

        
    


代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.ComponentModel;
namespace WpfApplication1
{
    /// 
    /// Window2.xaml 的交互逻辑
    /// 

    public partial class Window2 : Window,INotifyPropertyChanged
    {
        private string html = string.Empty;
        public string Html
        {
            get { return html; 
               }
            set { html = value;
                 OnPropertyChanged("Html");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {               
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName)) ;
            }
        }
        public Window2()
        {
            InitializeComponent();
            Html = "";
        }
    }
}
为什么Html的值无法传到控件中,不用wb.Con = "";   的方式


解决方案1:

主要原因是‘系统’对依赖属性的赋值,并不需要经过属性Setter。就是说下面的红色语句没有机会得到运行。
public string Con
{
    set
    {
        SetValue(ConProperty, value);
        wb.NavigateToString(value);
    }
    get
    {
        return (string)GetValue(ConProperty);
    }
}
因此,依赖属性Con改为:

public static readonly DependencyProperty ConProperty = DependencyProperty.Register(
    "Con", 
    typeof(string), 
    typeof(WebB), 
    new PropertyMetadata("", ConChanged));
private static void ConChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    (d as WebB).wb.NavigateToString(e.NewValue as string);
}

另外,主窗口的绑定没有DataContext,应加上以下粗体部分:
        public Window2()
        {
            InitializeComponent();
            this.DataContext = this;
            Html = "";
        }
以上介绍了“wpf 自定义控件绑定问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1172501.html

相关图片

相关文章