C# ItemsControl不更新ItemsSource绑定
我将C# ItemsControl不更新ItemsSource绑定,c#,wpf,xaml,C#,Wpf,Xaml,我将observedcollection绑定到ItemsControl作为ItemsSource,从VM到视图的绑定工作正常,但如果我更改文本框中的绑定内容,它将不会更新绑定到的observedcollection 我似乎不明白为什么,有人知道这是为什么吗 这是我的密码: <ItemsControl ItemsSource="{Binding Metrics, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Column="1
observedcollection
绑定到ItemsControl
作为ItemsSource
,从VM到视图的绑定工作正常,但如果我更改文本框中的绑定内容,它将不会更新绑定到的observedcollection
我似乎不明白为什么,有人知道这是为什么吗
这是我的密码:
<ItemsControl ItemsSource="{Binding Metrics, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Column="1" Grid.Row="1" Margin="0, 20, 0, 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal">
<TextBox Name="CalibrationNameTB" Grid.Column="1" Text="{Binding ., UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Style="{StaticResource baseStyle}" Margin="0, 1" Padding="5, 1" Width="270" FontSize="12"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl" >
<StackPanel Orientation="Horizontal" >
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
无法更新字符串,因为它是不可变的
您应该做的是将observateCollection
替换为observateCollection
,其中YourType
是一个具有公共string
属性的类,您可以获取或设置该属性:
class YourType : INotifyPropertyChanged
{
private string _theString;
public string TheString
{
get { return _theString; }
set { _theString = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
然后在XAML标记中绑定到此属性:
<WrapPanel Orientation="Horizontal">
<TextBox Name="CalibrationNameTB" Grid.Column="1" Text="{Binding TheString, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource baseStyle}" Margin="0, 1" Padding="5, 1" Width="270" FontSize="12"/>
</WrapPanel>
Text=“{Binding.}”
不会替换ItemsSource集合中的字符串。您必须使用带有字符串属性的包装器类。@克莱门斯,您能为我指出如何实现这一点的正确方向吗?不要使用带有字符串属性的MyItem类的ObservableCollection
,也要发布ViewModel代码请