我正在使用带有此datatemplate的列表框开发Windows Phone 8应用程序:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="LocalizationItemTemplate">
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="8" Background="#FF003847" Height="80">
<StackPanel x:Name="contentGrid" Margin="4" Orientation="Horizontal">
<CheckBox x:Name="selectedCheck" Visibility="{Binding CheckBoxVisibility}" IsChecked="{Binding IsChecked}"
HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center" IsEnabled="False"/>
<TextBlock x:Name="locationName" TextWrapping="Wrap" Text="{Binding Content}" VerticalAlignment="Center" FontSize="24" HorizontalAlignment="Left" Margin="10,0,0,0"/>
</StackPanel>
</Border>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
我使用ObservableCollection<LocationToShow>
作为ItemsSource
:
public class LocationToShow : ARItem
{
public double Distance { get; set; }
public string FormatedDistance
{
get
{
if (Distance == double.NaN)
return string.Empty;
else
return string.Format("{0} metros", Distance.ToString("N0"));
}
}
public Visibility CheckBoxVisibility { get; set; }
public bool IsChecked { get; set; }
}
但是,当我更改CheckBoxVisibility
或IsChecked
时,我看不到复选框或检查它。
如何解决此问题?
答案 0 :(得分:1)
LocationToShow
或ARItem
类需要实现INotifyPropertyChanged
接口,每次视图模型属性更改值时,您需要引发PropertyChanged
事件:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
_isChecked = value;
NotifyPropertyChanged("IsChecked");
}
}
}
答案 1 :(得分:0)
在您的班级INotifyPropertyChanged
上实施LocationToShow
,以通知用户界面更新任何属性更改。
请参阅此 - How to Implement INotifyPropertyChanged notification。
public class LocationToShow : ARItem, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
if (isChecked != value)
{
isChecked = value;
OnPropertyChanged("IsChecked");
}
}
}
}
更改所有其他属性以遵循相同的模式,以防您希望在类中的任何属性更改时刷新UI。