[英]C# WPF/DataGrid: Is there something that TextBox offers that a UserControl doesn't
我有一個UserControl,我想用它來編輯DataGrid中的數據。 UserControl具有用於綁定的TextProperty。 編輯單元格時,將顯示TextProperty的內容。 但是,當離開編輯模式時,我項目的TextPropery不會更新。
在這里,我找到了一個解決方案: C#Wpf編輯Datagrid不會更新它的itemsource
對於TextBox,此綁定有效。
factoryTextBox.SetBinding(TextBox.TextProperty, new Binding("Title"));
但是對於我的CustomTextBox,它僅適用於將模式設置為TwoWay模式。
factoryTextBox.SetBinding(CustomTextBox.TextProperty, new Binding("Title") {
Mode = BindingMode.TwoWay // <--
});
為什么我的用戶控件需要TwoWay,而TextBox卻不需要。 我想念什么嗎?
文本框的textproperty具有為其設置的元數據,使其默認情況下雙向綁定。 您想要非常大致的東西:
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"TextProperty",
typeof(string),
typeof(CustomTextBox),
new FrameworkPropertyMetadata(string.empty
, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.