2017-07-02 91 views
0

我有一个列表框,并在该列表框内,您可以选择项目。出于某种原因,没有可视化表示,如果可能的话,我想添加一个。我甚至没有看到默认的蓝色。根本不值一提。XAML列表框项目没有亮点

项目:WPF,使用XAML,C#和MVVM(MVVM Light)。 Visual Studio 2010中

的第一件事就是看看列表框本身:

<ListBox ItemsSource="{Binding NodeListViewModel.NodeList, Source={StaticResource Locator}}" Background="Transparent" Name="LbNodes"> 
      <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
        <Canvas HorizontalAlignment="Left" VerticalAlignment="Top" Width="1400" Height="1200"/> 
       </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Canvas.Left" Value="{Binding CanvasLeft}"/> 
       <Setter Property="Canvas.Top" Value="{Binding CanvasTop}"/> 
       <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lb_PreviewMouseLeftButtonDown" /> 
       </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
        <Canvas Background="Black"> 
         <Thumb Name="myThumb" Template="{StaticResource NodeVisualTemplate}"> 
          <i:Interaction.Triggers> 
           <i:EventTrigger EventName="DragDelta"> 
             <cmd:EventToCommand Command="{Binding NodeListViewModel.DragDeltaCommand, Source={StaticResource Locator}}" PassEventArgsToCommand="True"/> 
           </i:EventTrigger> 
          </i:Interaction.Triggers> 
         </Thumb> 
        </Canvas> 
       </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

所以它与含有帆布其中包含一个拇指数据模板的列表框。在“NodeVisualTemplate”如下:

<ControlTemplate x:Key="NodeVisualTemplate"> 
      <Border BorderThickness="2" BorderBrush="LightBlue" Margin="2" CornerRadius="5,5,5,5"> 
       <StackPanel> 
        <TextBlock Text="Test" Background="AntiqueWhite"/> 
        <TextBlock Text="{Binding Path=NodeText}" Background="Aqua"/> 
        <StackPanel Orientation="Horizontal"> 
         <TextBox Text="Type here" MinWidth="50"/> 
         <Image Source="{StaticResource ImgFolder}" Margin="0,0,5,0" Width="32" Height="32"/> 
        </StackPanel> 
       </StackPanel> 
      </Border> 
     </ControlTemplate> 

的问题,如前面提到的,是当一个选择项目,没有视觉突出显示状态。

问题1:是否真的选择了项目?

我这么认为。后面的代码包含此:

private void lb_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     ListBoxItem lbi = sender as ListBoxItem; 
     LbNodes.SelectedItem = lbi.DataContext; 

     //MessageBox.Show("Selected node name: " + ((lbi.DataContext) as NodeViewModel).NodeText); 
    } 

MessageBox的是一个小测试,让我检查选择代码正在运行,并且正确的项目被选中。它是。

问题2:你尝试这样的事:

<Style x:Key="myListboxStyle"> 
<Style.Resources> 
    <!-- Background of selected item when focussed --> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />     
    <!-- Background of selected item when not focussed --> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" /> 
</Style.Resources> 

为什么是我做到了。然后我添加Style =“{StaticResource myListboxStyle}”到我的ListBox,但没有改变。

问题3:你是否试过通过ItemContainerStyle做它?

当然有兄弟。 ItemContainerStyle从顶部显示的内容更改为:

<ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Canvas.Left" Value="{Binding CanvasLeft}"/> 
       <Setter Property="Canvas.Top" Value="{Binding CanvasTop}"/> 
       <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lb_PreviewMouseLeftButtonDown" /> 


        <Style.Triggers> 
         <Trigger Property="IsSelected" Value="True" > 
          <Setter Property="Background" Value="Red" /> 
         </Trigger> 
        </Style.Triggers> 
        <Style.Resources> 
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 
        </Style.Resources> 
       </Style> 
     </ListBox.ItemContainerStyle> 

没有变化。仍然没有看到任何亮点。

问题4:它看起来像什么?

像这样:

enter image description here

不要担心线 - 他们没有关系(我不认为是这样)。但是关于它们相关的变化,我有两个ListBox。首先是线条,它使用与拇指相同的数据。一个坐在另一个上面(大拇指在上面)。

谢谢你的时间。

回答

0

这里的问题是(我之前遇到过这个问题,很清楚我没有很好地学习),Canvas似乎默认为非常小的尺寸。因此,包含拇指的画布很小,即使节点本身可以​​清晰地看到。