![](/img/trans.png)
[英]ViewModel property not updating when it's corresponding CustomControl property is updated
[英]silverlight Textbox not updated when underlying collection's corresponding property is changed
舉個例子
我有2個文本框。 兩個文本框的文本都綁定到類Name {String fullname,String funnyName}; 它們不是直接綁定,而是由轉換器綁定
他們正在實現INotifyChanged
,綁定的DataContext
是ObservableCollection
和所有其他標准的東西。
綁定此模板,以便我在一行中有2個文本框,列表框有10行,問題是:
當我在文本框1中更改全名時,我去更改綁定集合中的funnyname。
這不會立即反映到GUI上。
我怎樣才能做到這一點? 我不想更新整個列表框,我不想直接將它綁定到我的類中的另一個屬性,而是通過轉換器。 當屬性從“TOm”變為“dick”時,不會調用轉換器,即轉換器僅被稱為第一次。 接下來每當一些屬性發生變化時
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("FunnyName"));
被調用,不調用轉換器。
添加了原始代碼
集合類
public class VariableData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
String _Source;
/// <summary>
/// Gets or sets the source.
/// </summary>
/// <value>
/// The source.
/// </value>
public String Source
{
get { return _Source; }
set
{
_Source = value; if (this.PropertyChanged != null)
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Source"));
}
}
}
綁定
<TextBox Name="textBoxFileLocation"
Text="{Binding Converter={StaticResource mapTypeToDataConverter}, ConverterParameter=41}"
Margin="5,5,5,5" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
</TextBox>
轉換器
public class MapTypeToDataConverter : IValueConverter
{
#region IValueConverter Members
/// <summary>
/// Modifies the source data before passing it to the target for display in the UI.
/// </summary>
/// <param name="value">The source data being passed to the target.</param>
/// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
/// <param name="parameter">An optional parameter to be used in the converter logic.</param>
/// <param name="culture">The culture of the conversion.</param>
/// <returns>
/// The value to be passed to the target dependency property.
/// </returns>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return "";
VariableData cus = null;
try
{
cus = (VariableData)value;
}
catch (Exception e3)
{
return null;
}
if (cus == null)
return "";
int temp = int.Parse(parameter.ToString());
int mapType = temp / 10;
int whatToreturn = temp % 10;
if (mapType != cus.MappingType)
{
if (whatToreturn == 3)
return false;
else
return "";
}
switch (whatToreturn)
{
case 1:
return cus.Source;
break;
case 2:
return cus.Query;
break;
case 3:
if (cus.Source != null && cus.Source.Length > 0)
return true;
else
return false;
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)value;
}
#endregion IValueConverter Members
}
你有沒有在XAML中設置Mode=TwoWay
:
<TextBox Text="{Binding MyProperty, Mode=TwoWay, Converter={StaticResource myConverter}}" />
(目前不在IDE上,因此可能存在拼寫錯誤)
這意味着當UI更改時,UI會在MyProperty
更改時更新以及更新MyProperty
更新。
您似乎將整個VariableData
綁定為值,然后使用轉換器提取所需的輸出。 由於VariableData
實例本身沒有改變(您的ConvertBack
沒有返回VariableData
類型的新對象),因此UI沒有理由相信它需要更新其UI。
您應該做的是刪除轉換器並綁定到VariableData
屬性,將您需要的邏輯移動到VariableData
根據需要創建其他屬性。 如果更改一個屬性也會影響另一個屬性,則可以確保為這兩個PropertyChanged
引發PropertyChanged
事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.