【发布时间】:2011-07-21 21:54:44
【问题描述】:
我正在尝试将 TextBlock 绑定到 ObservableCollection 中的特定元素。 这就是我现在所做的:
private ObservableCollection<double> arr = new ObservableCollection<double>();
public ObservableCollection<double> Arr { get { return arr; } set { arr = value; } }
testBox.DataContext = this;
private void Button_Click(object sender, RoutedEventArgs e)
{
Arr[0] += 1.0;
}
[ValueConversion(typeof(ObservableCollection<double>), typeof(String))]
public class myObsCollConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ObservableCollection<double> l = value as ObservableCollection<double>;
if( l == null )
return DependencyProperty.UnsetValue;
int i = int.Parse(parameter.ToString());
return l[i].ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
<Window.Resources>
<local:myObsCollConverter x:Key="myConverter"/>
</Window.Resources>
<TextBlock Name="testBox" Text="{Binding Path=Arr,Converter={StaticResource myConverter}, ConverterParameter=0}" />
我看到的是 testBox 在创建时显示了 Arr 的第一个值。 但这并不反映此元素的任何更改。 为了在我的文本框中看到对 Arr[0] 的更改,我需要做什么?
【问题讨论】:
标签: .net wpf xaml data-binding