列表视图更改所选项目的外观

时间:2015-03-03 14:08:56

标签: c# wpf

我有以下ListView

<ListView VerticalAlignment="Top" HorizontalAlignment="Left" Width="175"
        Height="200" Grid.Row="2" Name="StepsList" SelectionMode="Single" 
        ItemsSource="{Binding Steps}" Margin="0,5,0,0" Grid.RowSpan="2"
        SelectionChanged="StepsList_SelectionChanged"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
   <ListView.View>
       <GridView>
           <GridViewColumn Width="170" Header="Steps">
               <GridViewColumn.CellTemplate>
                  <DataTemplate>
                      <Grid Width="165" GotFocus="StepsGrid_OnGotFocus"
                          LostFocus="StepsGrid_OnLostFocus">
                         <Grid.ColumnDefinitions>
                              <ColumnDefinition Width="105"/>
                              <ColumnDefinition Width="15"/>
                              <ColumnDefinition Width="15"/>
                         </Grid.ColumnDefinitions>
                         <views:EditableTextBlock Grid.Column="0" 
                              Text="{Binding Number}" HorizontalAlignment="Left"/>
                         <Button Name="StepUp" Grid.Column="1"
                             Content="↑" HorizontalAlignment="Right"/>
                         <Button Grid.Column="2" Content="↓" HorizontalAlignment="Right"/>
                      </Grid>
                  </DataTemplate>
               </GridViewColumn.CellTemplate>
               </GridViewColumn>
               <GridView.ColumnHeaderContainerStyle>
                 <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="IsEnabled" Value="False"/>
                 </Style>
               </GridView.ColumnHeaderContainerStyle>
            </GridView>
     </ListView.View>
</ListView>

我想要的是,Button仅在选中项目时可见,如果所选项目是列表中最顶部/最底部项目,则禁用向上/向下按钮。
我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

这是使用MultiBinding的解决方案。你可能想要调整逻辑,正如Blam所说,我不认为你真的想在两个外边界点都禁用这两个按钮,但这就是你所说的......

XAML:

<Button Name="StepUp"  Grid.Column="1" Content="↑" HorizontalAlignment="Right"  >
                                    <Button.Visibility>
                                        <MultiBinding Converter="{StaticResource OuterboundsConverter}">
                                            <Binding Path="DataContext.Steps" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/>
                                            <Binding></Binding>
                                        </MultiBinding>
                                    </Button.Visibility>
                                </Button>
                                <Button Grid.Column="2" Content="↓" HorizontalAlignment="Right">
                                    <Button.Visibility>
                                        <MultiBinding Converter="{StaticResource OuterboundsConverter}">
                                            <Binding Path="DataContext.Steps" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/>
                                            <Binding></Binding>
                                        </MultiBinding>
                                    </Button.Visibility>
                                </Button>

转换器:

public class OuterboundsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        Visibility vis = Visibility.Visible;


        if (values != null)
        {
            ObservableCollection<string> steps = values[0] as ObservableCollection<string>;
            string item = values[1] as string;

            if (steps != null && item != null)
            {
                if (steps.Count > 0)
                {
                    if (steps[0] == item || steps[steps.Count - 1] == item)
                    {
                        vis = Visibility.Collapsed;
                    }
                }
            }
        }


        return vis;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

答案 1 :(得分:0)

我最终得到了以下内容 XML

<ListView VerticalAlignment="Top" HorizontalAlignment="Left" Width="175" Height="200" Grid.Row="2" Name="StepsList" SelectionMode="Single" 
                      ItemsSource="{Binding Steps}"
                      Margin="0,5,0,0" Grid.RowSpan="2" SelectionChanged="StepsList_SelectionChanged">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="170" Header="Steps">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid Width="165">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="105"/>
                                            <ColumnDefinition Width="15"/>
                                            <ColumnDefinition Width="15"/>
                                        </Grid.ColumnDefinitions>
                                        <views:EditableTextBlock Grid.Column="0"  Text="{Binding Number}" HorizontalAlignment="Left"/>
                                        <Button Name="StepUp"  Grid.Column="1" Content="↑" HorizontalAlignment="Right" Visibility="Hidden" Click="StepUp_OnClick"/>
                                        <Button Name="StepDown" Grid.Column="2" Content="↓" HorizontalAlignment="Right" Visibility="Hidden" Click="StepDown_OnClick"/>
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridView.ColumnHeaderContainerStyle>
                            <Style TargetType="{x:Type GridViewColumnHeader}">
                                <Setter Property="IsEnabled" Value="False"/>
                            </Style>
                        </GridView.ColumnHeaderContainerStyle>
                    </GridView>
                </ListView.View>
            </ListView>

C#

 private void StepsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _viewModel.SetStep(StepsList.SelectedIndex);

        var gridView = StepsList.View as GridView;
        var listBoxItem = StepsList.ItemContainerGenerator.ContainerFromItem(StepsList.SelectedItem) as ListBoxItem;
        var listBoxItems = Enumerable.Range(0, StepsList.Items.Count).Select(x => StepsList.ItemContainerGenerator.ContainerFromIndex(x) as ListBoxItem);
        var column = gridView.Columns[0];
        Button up;
        Button down;
        foreach (var contentPresenter in listBoxItems.Where(x => x != null).Select(x => x.GetVisualChild<ContentPresenter>()))
        {
            up = column.CellTemplate.FindName("StepUp", contentPresenter) as Button;
            down = column.CellTemplate.FindName("StepDown", contentPresenter) as Button;
            up.Visibility = Visibility.Hidden;
            down.Visibility = Visibility.Hidden;
            up.IsEnabled = true;
            down.IsEnabled = true;
        }

        if (listBoxItem == null) return;
        var myContentPresenter = listBoxItem.GetVisualChild<ContentPresenter>();

        up = column.CellTemplate.FindName("StepUp", myContentPresenter) as Button;
        down = column.CellTemplate.FindName("StepDown", myContentPresenter) as Button;
        if (up == null) return;
        up.Visibility = Visibility.Visible;
        if (Equals(_viewModel.Steps.First(), StepsList.SelectedItem))
            up.IsEnabled = false;
        if (down == null) return;
        down.Visibility = Visibility.Visible;
        if (Equals(_viewModel.Steps.Last(), StepsList.SelectedItem))
            down.IsEnabled = false;
    }