0

ListBox コントロールと、ListBox 用のいくつかの DataTemplates があります。

<DataTemplate DataType="{x:Type local:LogEntry}" x:Key="lineNumberTemplate">
  <Grid IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
      <ColumnDefinition SharedSizeGroup="Index" Width="Auto"/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Grid>
      <Rectangle Fill="{Binding Path=LineNumbersBackgroundColor, ElementName=LogViewerProperty}" Opacity="0.4" />
      <TextBlock Grid.Column="0" Margin="5,0,5,0" Style="{StaticResource MyLineNumberText}" x:Name="txtBoxLineNumbers" />
    </Grid>
    <TextBlock Grid.Column="1" Margin="5,0,0,0" Style="{StaticResource MyTextEditor}" />
  </Grid>
</DataTemplate>

<ListBox ItemsSource="{Binding}" x:Name="LogViewer" Background="{Binding Path=TextEditorBackgroundColor, ElementName=LogViewerProperty}" Style="{StaticResource MyListBox}" Foreground="{Binding Path=TextEditorForegroundColor, ElementName=LogViewerProperty}" ItemTemplateSelector="{StaticResource templateSelector}" Loaded="LogViewer_Loaded" SelectionMode="Extended" ItemContainerStyle="{StaticResource LeftAligned}" />

マウスの左ボタンが押され、マウスが TextBlock txtBoxLineNumbers 内にあるときに、実行時に ItemContainerStyle を変更することは可能ですか?

すべてのヒントは大歓迎です!

アップデート

わかりました、解決策を見つけました:

  var items = (sender as ListBox).SelectedItems;

  foreach (LogEntry item in items)
  {
    ListBoxItem myListBoxItem = (ListBoxItem) (LogViewer.ItemContainerGenerator.ContainerFromItem (item));
    ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter> (myListBoxItem);
    DataTemplate myDataTemplate = myContentPresenter.ContentTemplateSelector.SelectTemplate (myListBoxItem, LogViewer);

    if (!mouseMove && leftMouseButtonDown)
    {
      TextBlock target = (TextBlock) myDataTemplate.FindName ("txtBoxLineNumbers", myContentPresenter);

      if (target.IsMouseOver && leftMouseButtonDown)
      {
        System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream (new Uri ("/LogViewer;component/Template/RightArrow.cur", UriKind.Relative));
        Cursor = new Cursor (info.Stream);
        fullSelectionBox = true;

        LogViewer.ItemContainerStyle = null;
        break;
      }

      LogViewer.ItemContainerStyle = (Style) FindResource ("LeftAligned");
    }

効きそうです!

4

1 に答える 1