gpt4 book ai didi

c# - XAML 绑定(bind)未更新

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:38 25 4
gpt4 key购买 nike

我有一个 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 的类需要遵守 INotifyPropertyChangeIsCurrent 的 setter 需要启动属性更改通知。然后它将以手动和编程方式工作。

关于c# - XAML 绑定(bind)未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com