我有一个保存我的项目的GridView
组件。选择一个项目后,我要在其上弹出控件,以允许您编辑或删除该项目。这是我用于商品的DataTemplate
:
<Grid Height="200" Width="200">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Fill="White"/>
<TextBlock Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="No preview image" FontSize="18" Foreground="{ThemeResource TextBoxDisabledBorderThemeBrush}"/>
<Image Style="{StaticResource ProjectCoverImageStyle}" Grid.Row="0"/>
<TextBlock Style="{StaticResource ProjectTitleStyle}" Text="{Binding Title}" Grid.Row="1"/>
<!--The controls for editing and deleting. Show only when project is selected.-->
<Grid x:Name="ControlsGrid" Visibility="{Binding IsCurrent, Converter={StaticResource VisibilityConverter}}">
<Rectangle Grid.Row="0" Fill="Gray" Opacity="0.5"/>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<Border Style="{StaticResource RoundButtonStyle}">
<Image Source="/Assets/Icons/Edit.png" Style="{StaticResource RoundButtonIconStyle}"/>
</Border>
<Border Style="{StaticResource RoundButtonStyle}" Tapped="Delete_Project">
<Image Source="/Assets/Icons/Delete.png" Style="{StaticResource RoundButtonIconStyle}"/>
</Border>
</StackPanel>
</Grid>
</Grid>
我正在这样定义转换器:
<local:VisibilityConverter x:Key="VisibilityConverter"/>
VisibilityConverter是一个类:
public class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
bool visibility = (bool)value;
return visibility ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
Visibility visibility = (Visibility)value;
return (visibility == Visibility.Visible);
}
}
在
GridView
中选择一个项目即可运行:private void Projects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count < 1)
return;
foreach (Project project in ProjectsGridView.Items)
{
if (ProjectsGridView.SelectedItem != project)
project.IsCurrent = false;
else
{
project.IsCurrent = true;
ViewModel.CurrentProject = ViewModel.Projects[ProjectsGridView.SelectedIndex];
}
}
}
如果我将
IsCurrent
手动设置为true,则可以正常工作。但是,在选择/取消选择项目时它不会更新。我的代码正确设置了IsCurrent
,但是绑定似乎没有更新。还有什么需要做的吗? 最佳答案
如果我手动将IsCurrent设置为true,则可以正常工作
为了使xaml收到有关此类更改的通知,保存IsCurrent
的类需要遵守INotifyPropertyChange
,而IsCurrent
的设置者需要启动属性更改通知。然后它将以手动方式或以编程方式工作。
关于c# - XAML绑定(bind)未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32849766/