Skip to main content
 首页 » 编程设计

c#之如何在 WPF 中绑定(bind)和刷新 UserControl 绑定(bind)

2024年02月06日16lidabo

我正在尝试为用户控件做一个简单的 OneWay 绑定(bind),但老实说我不明白为什么有些部分不起作用。 为简单起见,我创建了一个具有一些依赖属性的 WPF 标准 UserControl。 在我的主窗口中,我根据某些事件隐藏/显示 UserControl,并将属性绑定(bind)到我的主窗口 View 模型数据。 但是,这样做不会让我的 UserControl 刷新某些部分。例如,我的 UserControl 上有一个 SearchString 依赖属性:

public static readonly DependencyProperty SearchStringProperty = DependencyProperty.Register("SearchString", 
           typeof(string), typeof(DisplayMailView), new UIPropertyMetadata(null)); 
 
public string SearchString 
    { 
        get { return (string)GetValue(SearchStringProperty); } 
        set 
        { 
            SetValue(SearchStringProperty, value); 
            if (PropertyChanged != null) 
            { 
                PropertyChanged(this, new PropertyChangedEventArgs("SearchString")); 
            } 
            var loFinds = richEditControl1.Document.FindAll(SearchString, SearchOptions.WholeWord); 
 
            foreach (var find in loFinds) 
            { 
                var oDoc = find.BeginUpdateDocument(); 
                var oChars = oDoc.BeginUpdateCharacters(find); 
                oChars.BackColor = System.Drawing.Color.Yellow; 
                oDoc.EndUpdateCharacters(oChars); 
                find.EndUpdateDocument(oDoc); 
            } 
        } 
    } 

UserControl 中的这个绑定(bind)到我的主窗口 View 模型的 SearchString 成员

<local:DisplayMailView SearchString="{Binding DataContext.SearchString, RelativeSource={RelativeSource AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True,Mode=TwoWay}"/> 

在我的 ViewModel 上:

 public string SearchString 
    { 
        get 
        { 
            return _searchString; 
        } 
        set 
        { 
            _searchString = value; 
            if (!string.IsNullOrWhiteSpace(value)) 
                DataGridService.FocusFirstRow(); 
            RaisePropertyChanged(); 
        } 
    } 

我在主窗口 View 模型中的 SearchString 集合中放置了一个断点,该值已设置。但是,当我在 UserControl 上的 SearchString 集合中放置一个断点时,它永远不会触发?

此外,我的 UserControl 的 dataContext 已设置:

    (this.Content as FrameworkElement).DataContext = this; 

我明确指出,我的 UserControl 构造函数仅在启动时调用一次,其他内容必须根据用户操作动态刷新。

有人有想法吗?我在输出窗口中没有错误 谢谢

请您参考如下方法:

这是一个常见的误解。当 DependencyProperty 的值发生变化时,不会执行其 CLR 属性的 set 中的代码。如果您想在属性值更改时执行某些代码,则需要使用 PropertyChanged 回调。

您的 UserControl 代码应该如下所示:

public static readonly DependencyProperty SearchStringProperty = 
    DependencyProperty.Register( 
        "SearchString", 
        typeof(string), 
        typeof(DisplayMailView), 
        new UIPropertyMetadata(null, OnSearchStringChanged)); 
        // This sets OnSearchStringChanged as the PropertyChanged callback 
 
public string SearchString 
{ 
    get { return (string)GetValue(SearchStringProperty); } 
    set 
    { 
        SetValue(SearchStringProperty, value); 
        //  Any code you put here won't be executed  
        // when the DependencyProperty value changes 
    } 
} 
 
private static void OnSearchStringChanged(object sender, DependencyPropertyChangedEventArgs e) 
{ 
    //  This part is not needed, DependencyProperties already 
    // notify of their changes automatically 
    //if (PropertyChanged != null) 
    //{ 
    //    PropertyChanged(this, new PropertyChangedEventArgs("SearchString")); 
    //} 
 
    var control = sender as DisplayMailView;      
 
    var loFinds = control.richEditControl1.Document.FindAll(SearchString, SearchOptions.WholeWord); 
 
     foreach (var find in loFinds) 
     { 
         var oDoc = find.BeginUpdateDocument(); 
         var oChars = oDoc.BeginUpdateCharacters(find); 
         oChars.BackColor = System.Drawing.Color.Yellow; 
         oDoc.EndUpdateCharacters(oChars); 
         find.EndUpdateDocument(oDoc); 
     } 
}