更改用戶控件 DependencyProperty 時屬性重置 (Property resets when changing Usercontrols DependencyProperty)


問題描述

更改用戶控件 DependencyProperty 時屬性重置 (Property resets when changing Usercontrols DependencyProperty)

我已經構建了一個“經理”;“小部件” 我有一個帶有小部件的 ListView/GridView。

ListView 將 SelectedItem 綁定到(管理器)ViewModel 上的 SelectedWidget 屬性。

SelectedWidget 通過一個名為 WidgetConfig 的用戶控件綁定DependencyProperty 稱為“Widget”。您可以在其中修改所選 Widget 的屬性。

在 WidgetConfig 中,有 2 個 RadioButtons:

<RadioButton GroupName="Lead" IsChecked="{Binding Widget.prop1, Mode=TwoWay, ElementName=root}" />
<RadioButton GroupName="Lead" IsChecked="{Binding Widget.prop2, Mode=TwoWay, ElementName=root}" />

當我選擇“prop2”時,INPC 信號被正確發送並且模型會相應更新。如果然後我單擊“prop1”,則發送 INPC 信號 + 我從 prop2 單選按鈕獲得一個附加信號。

問題出在:如果 我選擇 prop2 然後在 ListView 中選擇另一個 Widget。我收到一個 INPC 信號,prop2 變回 false

發生了什麼事?

一些信息:

  • WidgetConfiguration 是使用 PostSharp NotifyPropertyChanged 方面實現的。
  • WidgetConfig UserControl 是使用 DependencyProperty 和 INotifyPropertyChanged 接口手動實現的

編輯:

I在這裡創建了一個可重現的示例:https://github.com/Montago/INCP‑DP‑綁定示例


參考解法

方法 1:

What you have is a chain of two‑way bindings, which are changing values on your Model:

Two‑way binding between ListControl.SelectedItem(DP) and the ViewModel.SelectedModel(INPC).

Two‑way binding between ViewModel.SelectedModel(INPC) and ModelControl.ActiveModel(DP).

Two‑way binding between ModelControl.ActiveModel(DP) and Model.Prop1/Model.Prop2/Model.Name(INPC).

When you select First, Second and then again First, Second's prop2 is overwritten to false by WPF data binding.

A way to fix this is to remove the two‑way binding that makes the overwrite of prop2, i.e. specify Mode=OneWay on RadioButton bindings.

My guess of what is happening: RadioButton's logic and two‑way binding. When Second is selected in the list box and you change the selection to First, WPF's data binding sets Prop1 radio button as selected (because First.prop1 == true). At that point, the second radio button is still selected, so to respect the group principle, it needs to get unselected. However, there is still the two‑way binding active on the Prop2 radio and thus Second.prop2 gets set to false.

(by Martin KirkDaniel Balas)

參考文件

  1. Property resets when changing Usercontrols DependencyProperty (CC BY‑SA 2.5/3.0/4.0)

#mvvm #postsharp #wpf #inotifypropertychanged #dependency-properties






相關問題

WPF View 在關閉時將 ViewModel 屬性設置為 null (WPF View sets ViewModel properties to null on closing)

在文本框中正確輸入後啟用按鈕 (Button Enable After Correct Input In TextBox)

WPF說數據項不為空時為空 (WPF says that data item is null when it is not null)

MVVM - 如何將 ViewModel 包裝在 ViewModel 中? (MVVM - How to wrap ViewModel in a ViewModel?)

關於服務參考和 MVVM 模式的幾個一般問題 (A few general questions about Service Reference and MVVM pattern)

WPF MVVM 鏈接視圖 (WPF MVVM Linked Views)

wpf 樹視圖 mvvm (wpf treeview mvvm)

如何在 MVVM Light 的 ListView 中的 ComboBox 中顯示列表? (How to show a List in a ComboBox in a ListView in MVVM Light?)

多次調用 PropertyChanged 的 ViewModel 屬性 (ViewModel properties with multiple calls to PropertyChanged)

如何將圖像存儲在類庫中並從任何類訪問它 (How can i store an image in a class library and access it from any class)

Silverlight MVVM 隔離存儲 (Silverlight MVVM Isolated Storage)

如何將文本框的borderBrush屬性綁定到viewmodel中的屬性,類型轉換錯誤 (How to bind the borderBrush property of a textbox to a property in viewmodel, type conversion error)







留言討論