ObservableCollection의 특정 요소에 TextBlock을 바인딩하려고합니다. 내가 볼 것은 만들 때 testBox는 ARR의 첫 번째 값을 보여주고 있다는 점이다배열 요소에 바인딩
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}" />
: 이것은 내가 지금하는 일이다. 그러나이 요소에 대한 변경 사항은 반영되지 않습니다. 내 textBox에서 Arr [0] 변경 사항을 보려면 어떻게해야합니까?
그래도 어쨌든 작동하지 않아야합니까? ObservableCollection은 값이 대체 될 때 CollectionChanged 이벤트를 발생시킵니다 ... 바인딩이이를 인식하지 못함에 놀랍습니다. –
글쎄, UI가 PropertyChanged 이벤트 만 듣기 때문에 컬렉션이 아닌 요소에 바인딩합니다. – ChrisWue
요소 인덱스 (0)를 다른 컨트롤에 바인딩하지 않으면 어떻게됩니까? 예 : 필자는 testBox가 표시해야하는 요소의 색인을 선택하는 ComboBox를 가지고있다. – MTR