所以我有一个 ListView 。它有一个 ItemSource 和一个 SelectedItem 。
SelectedItem有一个bool属性,用于切换按钮和 TextBox 的可见性。
当你按下 ListViewItem 时,我希望能够打开和关闭可见性,即使我只是垃圾邮件行。
解决方案部分有效,只有当项目没有焦点时才会触发 SelectedItem 。因此,当我切换一次时,我必须再次切换第一个项目。
我考虑过代码隐藏,在 GotFocus 方法中添加一些内容,但无法想到我必须在那里做些什么。
建议
XAML:
<ListView ScrollViewer.VerticalScrollMode="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.IsVerticalRailEnabled="False"
Background="White"
ItemsSource="{Binding Activities}"
SelectedItem="{Binding SelectedActivity, Mode=TwoWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
ContentTransitions="{TemplateBinding ContentTransitions}"
SelectionCheckMarkVisualEnabled="True"
CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
PlaceholderBackground="White"
PointerOverBackground="LightGray"
PointerOverForeground="DarkGray"
SelectedBackground="White"
SelectedForeground="DimGray"
SelectedPointerOverBackground="White"
PressedBackground="White"
SelectedPressedBackground="White"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
ContentMargin="{TemplateBinding Padding}"
CheckMode="Inline"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.Header>
<StackPanel Background="#8c8c8c"
Orientation="Horizontal"
FlowDirection="LeftToRight"
Padding="8,8,8,8">
<TextBlock Text="{Binding Title}"
FontSize="18"
Foreground="White"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="10,0,0,0"/>
</StackPanel>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,10,0,10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical"
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="6,2,6,2">
<TextBlock FontSize="20"
VerticalAlignment="Center"
Foreground="DimGray"
TextWrapping="WrapWholeWords"
Text="{Binding Activity.Description}"/>
<TextBlock FontSize="15"
VerticalAlignment="Center"
Foreground="DimGray"
Text="{Binding Activity.Condition}"/>
</StackPanel>
<Button Grid.Row="0"
Grid.Column="1"
Grid.RowSpan="2"
Margin="6"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Command="{Binding ToggleMeasureCompletionCommand}">
<Grid>
<TextBlock FontFamily="Segoe MDL2 Assets"
FontSize="35"
Foreground="DimGray"
Text=""
Margin="8"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Visibility="{Binding IsCompleted, Converter={StaticResource InverseBooleanToVisibilityConverter}}"/>
<TextBlock FontFamily="Segoe MDL2 Assets"
FontSize="35"
Foreground="DimGray"
Text=""
Margin="8"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Visibility="{Binding IsCompleted, Converter={StaticResource BoolToVis}}"/>
</Grid>
</Button>
<Grid Visibility="{Binding IsInFocus, Converter={StaticResource BoolToVis}}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" PlaceholderText="Kommentar til tiltak" HorizontalAlignment="Stretch" Padding="5" Margin="0,0,10,0"/>
<Button Grid.Column="1" Content="Flag" HorizontalAlignment="Right" Foreground="White" Padding="10,5,10,5">
<Button.Background>
<SolidColorBrush Color="Red" Opacity="0.5" />
</Button.Background>
</Button>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C# - 属性:
public SelectedItemViewModel SelectedActivity
{
get { return _selectedActivity; }
set
{
_selectedActivity = value;
SelectedActivity.IsInFocus = !SelectedActivity.IsInFocus;
OnPropertyChanged();
}
}
答案 0 :(得分:1)
在listview的ItemClick事件中编写此代码SelectedActivity.IsInFocus = !SelectedActivity.IsInFocus
。
你也可以在录音内写活动