WPF ListBox DataTemplate Mouse里面

时间:2013-11-08 10:56:58

标签: c# wpf listbox datatemplate

我有一个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");
    }

似乎有效!

1 个答案:

答案 0 :(得分:0)

您可以使用控件模板设置为文本块的按钮,而不是文本块,如下所示:

                            <Button MouseLeftButtonDown="Button_MouseLeftButtonDown_1">
                            <Button.Template>
                                <ControlTemplate>
                                    <TextBlock Grid.Column="0" Margin="5,0,5,0" Style="{StaticResource MyLineNumberText}" x:Name="txtBoxLineNumbers"/>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>

然后在代码背后,您将需要:

 private void Button_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            var textblock = (sender as Button).Template.FindName("txtBoxLineNumbers", (sender as FrameworkElement)) as TextBlock;
            textblock.Text = "Hello World";
        }