动态更改ListView项绑定
本文关键字:绑定 ListView 动态 | 更新日期: 2023-09-27 18:22:03
我正在用Visual Studio 2015为应用商店制作Windows 8.1。我有带GridView的ListView,它运行得很好。在某些列中有TextBlock,它的Text是Bind,但我想在选中单选按钮上用不同的字段更改绑定。下面是我的代码。
<ListView Name="lvData" Margin="5,0" HorizontalAlignment="Stretch" Grid.Row="2" BorderBrush="Black"
BorderThickness="1" Foreground="#000" VerticalAlignment="Center">
<ListView.HeaderTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<GridViewHeaderItem Grid.Column="0" x:Uid="Number" Style="{StaticResource GridViewHeader}" />
<GridViewHeaderItem Grid.Column="1" x:Uid="_Dept" Style="{StaticResource GridViewHeader}" />
<GridViewHeaderItem Grid.Column="2" x:Uid="Arr" Style="{StaticResource GridViewHeader}" />
<GridViewHeaderItem Grid.Column="3" x:Uid="CurrentStatus" Style="{StaticResource GridViewHeader}" />
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<GridViewItem Grid.Column="0" Style="{StaticResource GridViewItem}" Content="{Binding Number}"/>
<GridViewItem Grid.Column="1" Style="{StaticResource GridViewItem}">
<StackPanel>
<TextBlock x:Name="DepTime" Text="{Binding DeptTime}" TextWrapping="Wrap" MaxWidth="65"/>
<TextBlock Text="" TextWrapping="Wrap"/>
<TextBlock x:Uid="Actual" TextWrapping="Wrap" MaxWidth="65" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding DeptStn}" Foreground="SkyBlue" TextWrapping="Wrap" MaxWidth="65" HorizontalAlignment="Center"/>
</StackPanel>
</GridViewItem>
<GridViewItem Grid.Column="2" Style="{StaticResource GridViewItem}">
<StackPanel>
<TextBlock x:Name="ArrTime" Text="{Binding ArrTime}" TextWrapping="Wrap" MaxWidth="65"/>
<TextBlock Text="" TextWrapping="Wrap"/>
<TextBlock x:Uid="Actual" TextWrapping="Wrap" MaxWidth="65" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding ArrivCode}" Foreground="SkyBlue" TextWrapping="Wrap" MaxWidth="65" HorizontalAlignment="Center"/>
</StackPanel>
</GridViewItem>
<GridViewItem Grid.Column="3" Style="{StaticResource GridViewItem}" Foreground="{Binding Color}" >
<TextBlock Text="{Binding CurrentStatus}" TextWrapping="Wrap" MaxWidth="60" HorizontalAlignment="Center"/>
</GridViewItem>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在上面的代码中,列索引1和2有TextBlock文本绑定,我想在单选按钮检查事件时更改它。
我走错了方向,@Romasz给出的保持可见的逻辑是绝对正确的,但我仍然走错了从代码后面访问控件的道路。但它不能从代码背后访问,因为它在DataTemplate下。
因此,我创建了布尔值IValueConverter,如下所示
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if ((bool)value)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
然后我在页面中添加了以下内容。资源
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
然后在实际的TextBlock 中
<TextBlock x:Name="DepTime" Text="{Binding Time}" TextWrapping="Wrap" MaxWidth="65" Visibility="{Binding ElementName=LocalTime, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>
在上面的ElementName=LocalTime,因为我的单选按钮名称是LocalTime
希望它能帮助