2017-07-02 39 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似乎默認爲非常小的尺寸。因此,包含拇指的畫布很小,即使節點本身可以​​清晰地看到。

相關問題