我有一个 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
,但绑定(bind)似乎没有更新。还有什么需要做的吗?
最佳答案
If I set IsCurrent to true manually, it works just fine
要向 xaml 通知此类更改,保存 IsCurrent
的类需要遵守 INotifyPropertyChange
以及 IsCurrent
的 setter需要启动属性更改通知。然后它将以手动和编程方式工作。
关于c# - XAML 绑定(bind)未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849766/