动态更改ListView项绑定

时间:2015-11-16 16:25:26

标签: c# listview silverlight windows-phone-8.1

我正在使用Visual Studio 2015为应用程序商店制作Windows 8.1。我有使用GridView的ListView,它运行良好。在某些列中有TextBlock,它的Text是Bind,但我想在Radio Button Checked上用不同的字段更改绑定。以下是我的代码。

<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文本绑定,我想在单选按钮检查事件上更改。

1 个答案:

答案 0 :(得分:0)

嗯,我的方向错误,@ 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();
        }
    }

然后我在 Page.Resources

中添加了以下内容
<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

希望它会对某人有所帮助