【发布时间】:2014-11-13 10:04:38
【问题描述】:
我在刷新与对象中属性的绑定时遇到问题。在启动时它显示了很好的结果,但是在我编辑源之后,我的窗口中的值没有刷新。
public class Marker : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private ObservableCollection<bool?> _repeat;
[...]
public ObservableCollection<bool?> Repeat
{
get { return _repeat; }
set
{
_repeat = value;
OnPropertyChanged(new PropertyChangedEventArgs("Repeat"));
OnPropertyChanged(new PropertyChangedEventArgs("RepeatString"));
}
}
[...]
[XmlIgnore]
public string RepeatString
{
get
{
string wt = string.Empty;
wt += (bool)Repeat[0] ? "Mo, " : string.Empty;
wt += (bool)Repeat[1] ? "Di, " : string.Empty;
wt += (bool)Repeat[2] ? "Mi, " : string.Empty;
wt += (bool)Repeat[3] ? "Do, " : string.Empty;
wt += (bool)Repeat[4] ? "Fr, " : string.Empty;
wt += (bool)Repeat[5] ? "Sa, " : string.Empty;
wt += (bool)Repeat[6] ? "So, " : string.Empty;
if (wt != string.Empty)
{
wt = wt.Remove(wt.Length - 2);
}
return wt;
}
[...]
}
[...]
}
在 XAML 部分中,我将绑定设置如下:
<Grid DataContext="{Binding ElementName=lvMarker, Path=SelectedItem}">
[...]
<CheckBox Content="Montag" IsChecked="{Binding Repeat[0], Mode=TwoWay}"/>
<CheckBox Content="Dienstag" IsChecked="{Binding Repeat[1], Mode=TwoWay}"/>
<CheckBox Content="Mittwoch" IsChecked="{Binding Repeat[2], Mode=TwoWay}"/>
<CheckBox Content="Donnerstag" IsChecked="{Binding Repeat[3], Mode=TwoWay}"/>
<CheckBox Content="Freitag" IsChecked="{Binding Repeat[4], Mode=TwoWay}"/>
<CheckBox Content="Samstag" IsChecked="{Binding Repeat[5], Mode=TwoWay}"/>
<CheckBox Content="Sonntag" IsChecked="{Binding Repeat[6], Mode=TwoWay}"/>
</StackPanel>
</Grid>
当我单击复选框时,这可以很好地更新数组。但不是以下文本块绑定。
<ListView x:Name="lvMarker" ItemsSource="{Binding MarkerCollection}" Width="Auto" ItemContainerStyle="{StaticResource lvItemStyle}">
<ListView.View>
<GridView>
[...]
<GridViewColumn Header="Zeit" Width="Auto" CellTemplate="{StaticResource DateCell}"/>
</GridView>
</ListView.View>
</ListView>
...
<DataTemplate x:Key="DateCell">
<StackPanel>
<TextBlock FontSize="14">
[...]
</TextBlock>
<TextBlock FontSize="10" Text="{Binding RepeatString, Mode=OneWay}"/>
</StackPanel>
</DataTemplate>
当我更改布尔值时,我需要做什么来刷新代表 RepeatString 的 TextBlock?重复次数[]?
【问题讨论】: