我可以在我的应用程序中使用C#应用程序的标记栏组件吗?作为标记栏,我的意思是像ReSharper这样的东西添加到了Visual Studio中:

另一个类似的示例(左侧的栏):

编辑:我发现java http://www.sideofsoftware.com/marker_bar/doc/sos/marker/JMarkerBar.html的非免费组件究竟是什么,我想做什么。它不适合我,但也许可以帮助某人。

最佳答案

在WPF中,该条有点像ListBox,只是以不同的方式为每行文本显示1像素高的行。线的状态将影响线的颜色,选择线将引发文本框可以响应的SelectionChanged事件。

如果您要我展示原型(prototype),请告诉我。

编辑

开始。您可以单击/选择栏中的一行,文本框将滚动到该行。

仍要添加:

  • 如果栏的行数太大,该怎么办?
  • 以其他方式显示栏中的当前行吗?
  • 使栏中的所选行与文本框中的插入符号保持同步。
  • ...

  • 这些可以解决,但在很大程度上取决于您想要什么。这应该可以帮助您入门。

    XAML:
    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication2"
            Title="MainWindow"
            Height="350"
            Width="525">
        <Window.Resources>
            <local:StatusToBrushConverter x:Key="statusToBrushConverter" />
        </Window.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <ListBox ItemsSource="{Binding}"
                     SelectionChanged="ListBox_SelectionChanged">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment"
                                Value="Stretch" />
                        <Setter Property="Opacity"
                                Value="0.5" />
                        <Setter Property="MaxHeight"
                                Value="1" />
                        <Setter Property="MinHeight"
                                Value="1" />
                        <Style.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Trigger.Setters>
                                    <Setter Property="Opacity"
                                            Value="1.0" />
                                </Trigger.Setters>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
    
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Rectangle StrokeThickness="0"
                                   Stroke="Green"
                                   Fill="{Binding Status, Converter={StaticResource statusToBrushConverter}}"
                                   Height="1"
                                   HorizontalAlignment="Stretch" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <TextBox AcceptsReturn="True"
                     Grid.Column="1"
                     x:Name="codeBox" />
        </Grid>
    </Window>
    

    C#:
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApplication2
    {
        public partial class MainWindow : Window
        {
            private CodeLines lines;
    
            public MainWindow()
            {
                InitializeComponent();
    
                lines = new CodeLines();
    
                Random random = new Random();
                for (int i = 0; i < 200; i++)
                {
                    lines.Add(new CodeLine { Status = (VersionStatus)random.Next(0, 5), Line = "Line " + i });
                }
    
                this.DataContext = lines;
    
                codeBox.Text = String.Join("\n",  from line in lines
                                                select line.Line);
            }
    
            private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var selectedLine = ((ListBox)sender).SelectedIndex;
                codeBox.ScrollToLine(selectedLine);
            }
        }
    
        public enum VersionStatus
        {
            Original,
            Added,
            Modified,
            Deleted
        }
    
        public class CodeLine : INotifyPropertyChanged
        {
    
            private VersionStatus status;
    
            public VersionStatus Status
            {
                get { return status; }
                set
                {
                    if (status != value)
                    {
                        status = value;
                        OnPropertyChanged("Status");
                    }
                }
            }
    
            private string line;
    
            public string Line
            {
                get { return line; }
                set
                {
                    if (line != value)
                    {
                        line = value;
                        OnPropertyChanged("Line");
                    }
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName)
            {
                var p = PropertyChanged;
                if (p != null)
                {
                    p(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    
        public class CodeLines : ObservableCollection<CodeLine>
        {
        }
    
    
        class StatusToBrushConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var status = (VersionStatus)value;
                switch (status)
                {
                    case VersionStatus.Original:
                        return Brushes.Green;
                    case VersionStatus.Added:
                        return Brushes.Blue;
                    case VersionStatus.Modified:
                        return Brushes.Yellow;
                    case VersionStatus.Deleted:
                        return Brushes.Red;
                    default:
                        return DependencyProperty.UnsetValue;
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    关于c# - C#应用程序的标记栏?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6083520/

    10-13 07:58