C# silverlight文本框在基础集合';的相应属性已更改
例如 我有两个文本框。两个文本框的文本都绑定到类名{String fullname,String funnyName};它们不是直接绑定的,而是由转换器绑定的 他们正在实现C# silverlight文本框在基础集合';的相应属性已更改,c#,silverlight,binding,observablecollection,converter,C#,Silverlight,Binding,Observablecollection,Converter,例如 我有两个文本框。两个文本框的文本都绑定到类名{String fullname,String funnyName};它们不是直接绑定的,而是由转换器绑定的 他们正在实现INotifyChanged,绑定的DataContext是observedcollection和所有其他标准内容 这个模板是绑定的,因此我在一行中有两个文本框,列表框有10行 问题是: 当我在文本框1中更改fullname时,我会在绑定集合中更改funnyname 这不会立即反映到GUI上 我怎样才能做到这一点?我不想更新整
INotifyChanged
,绑定的DataContext
是observedcollection
和所有其他标准内容
这个模板是绑定的,因此我在一行中有两个文本框,列表框有10行
问题是:
当我在文本框1中更改fullname时,我会在绑定集合中更改funnyname
这不会立即反映到GUI上
我怎样才能做到这一点?我不想更新整个列表框,也不想直接将其绑定到类中的另一个属性,而是通过转换器。当属性从say“TOm”更改为“dick”时,不调用转换器,即仅第一次调用转换器。下一步,每当某些属性发生更改时
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("FunnyName"));
则不调用转换器
添加了原始代码
收藏课
public class VariableData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
字符串源
/// <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"));
}
}
}
//
///获取或设置源。
///
///
///消息来源。
///
公共字符串源
{
获取{return\u Source;}
设置
{
_Source=值;if(this.PropertyChanged!=null)
this.PropertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(“源”);
}
}
}
绑定
<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
}
公共类MapTypeToDataConverter:IValueConverter
{
#区域转换器成员
///
///在将源数据传递到目标以在UI中显示之前修改源数据。
///
///正在传递到目标的源数据。
///目标依赖项属性所需的数据类型。
///在转换器逻辑中使用的可选参数。
///转换的文化。
///
///要传递给目标依赖项属性的值。
///
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
如果(值==null)
返回“”;
variabledatacus=null;
尝试
{
cus=(可变数据)值;
}
捕获(例外e3)
{
返回null;
}
如果(cus==null)
返回“”;
int temp=int.Parse(parameter.ToString());
int mapType=温度/10;
int whatToreturn=温度%10;
if(mapType!=cus.MappingType)
{
如果(whatToreturn==3)
返回false;
其他的
返回“”;
}
开关(whatToreturn)
{
案例1:
返回cus.Source;
打破
案例2:
返回cus.Query;
打破
案例3:
如果(cus.Source!=null&&cus.Source.Length>0)
返回true;
其他的
返回false;
}
返回“”;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回(int)值;
}
#端域转换器成员
}
您是否在XAML中设置了模式=双向
:
<TextBox Text="{Binding MyProperty, Mode=TwoWay, Converter={StaticResource myConverter}}" />
(目前没有IDE,因此可能会出现拼写错误)
这意味着当
MyProperty
更改时,UI会更新,当UI更改时,MyProperty
也会更新。您似乎将整个变量数据
绑定为值,然后使用转换器提取所需的输出。由于VariableData
实例本身没有改变(您的ConvertBack
没有返回类型为VariableData
的新对象),因此UI没有理由认为需要更新其UI
您应该做的是放下转换器并绑定到
VariableData的属性
将需要的逻辑移动到VariableData
中,根据需要创建其他属性。如果更改一个属性也会影响另一个属性,则可以确保为这两个属性引发PropertyChanged
事件。no。。仍然不是双向的。。。假设我的datacontext集合中有10个对象。。。然后我编辑第1行对象属性并单击“提交”。。单击处理程序将转到并更新第1行对象属性以及第5行和第10行对象属性。。。我希望gui反映对第5行和第10行所做的更改。。。请注意,所有文本属性都绑定到converter…@pskk-您可以将xaml编辑到问题中(显示集合的位)吗。我无法想象这个问题。