Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以绑定任何textcontrol的前景色_C#_.net_Wpf_Mvvm_Data Binding - Fatal编程技术网

C# 是否可以绑定任何textcontrol的前景色

C# 是否可以绑定任何textcontrol的前景色,c#,.net,wpf,mvvm,data-binding,C#,.net,Wpf,Mvvm,Data Binding,所以我三天来一直在想这个问题,但似乎找不到解决办法 这就是我想要达到的目标。 我有一个简单的WPF项目,里面有一个RichTextBox。 我的应用程序所做的是,它的行为类似于CMD 我现在要做的是,我想更改它在按enter键时保存的消息,我想更改上一条消息的颜色 这是一个GIF显示它看起来像什么 我试着将文本框的前景绑定到DataTemplate中,但这只会使文本无法显示 <DataTemplate> <TextBlo

所以我三天来一直在想这个问题,但似乎找不到解决办法

这就是我想要达到的目标。 我有一个简单的WPF项目,里面有一个RichTextBox。 我的应用程序所做的是,它的行为类似于CMD

我现在要做的是,我想更改它在按enter键时保存的消息,我想更改上一条消息的颜色

这是一个GIF显示它看起来像什么

我试着将文本框的前景绑定到DataTemplate中,但这只会使文本无法显示

<DataTemplate>
                            <TextBlock Text="{Binding Path=.}" Foreground="White" Name="SavedBlocks" FontFamily="Consolas"/>
                        </DataTemplate>
ConsoleContent.cs

public class ConsoleContent : INotifyPropertyChanged
{
    string consoleInput = string.Empty;
    ObservableCollection<string> consoleOutput = new ObservableCollection<string>() { "Console Emulation Sample..." };

    public string ConsoleInput
    {
        get
        {
            return consoleInput;
        }
        set
        {
            consoleInput = value;
            OnPropertyChanged("ConsoleInput");
        }
    }

    public ObservableCollection<string> ConsoleOutput
    {
        get
        {
            return consoleOutput;
        }
        set
        {
            consoleOutput = value;
            OnPropertyChanged("ConsoleOutput");
        }
    }

    public void RunCommand()
    {
        ConsoleOutput.Add(ConsoleInput);
        //myBrush = Brushes.Orange;
        // do your stuff here.
        ConsoleInput = String.Empty;
    }



    private System.Windows.Media.Brush _foregroundColor = System.Windows.Media.Brushes.DarkSeaGreen;

    public System.Windows.Media.Brush ForegroundColor
    {
        get { return _foregroundColor; }
        set
        {
            _foregroundColor = value;
            OnPropertyChanged("ForegroundColor");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
将consoleOutput的类型从ObservaleCollection更改为ObservaleCollection,其中YourType是一个用文本字符串和前景笔刷表示输入行的类:

绑定到XAML中此类的属性:

<ScrollViewer Name="Scroller" Margin="0" Background="Black">
    <StackPanel>
        <ItemsControl ItemsSource="{Binding ConsoleOutput, Mode=OneWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Text}" 
                               Foreground="{Binding Foreground}" 
                               Name="SavedBlocks" FontFamily="Consolas"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <TextBox Text="{Binding ConsoleInput, Mode=TwoWay}" Background="Black" Foreground="White" FontFamily="Consolas" Name="InputBlock" BorderBrush="{x:Null}" SelectionBrush="{x:Null}" />
    </StackPanel>
</ScrollViewer>

将ObservaleCollection更改为ObservaleCollection,并设置并绑定到YourType上的前台属性?@mm8但它只会打印出这个,我觉得我需要打印类的属性,现在它似乎正在打印类。当然,需要为这个类型更新数据模板。在您自己的类型上,您应该重写ToString方法以返回属性值。@NerdzIT:请看我的答案。这很有道理!非常感谢。
public class ConsoleContent : INotifyPropertyChanged
{
    string consoleInput = string.Empty;
    ObservableCollection<string> consoleOutput = new ObservableCollection<string>() { "Console Emulation Sample..." };

    public string ConsoleInput
    {
        get
        {
            return consoleInput;
        }
        set
        {
            consoleInput = value;
            OnPropertyChanged("ConsoleInput");
        }
    }

    public ObservableCollection<string> ConsoleOutput
    {
        get
        {
            return consoleOutput;
        }
        set
        {
            consoleOutput = value;
            OnPropertyChanged("ConsoleOutput");
        }
    }

    public void RunCommand()
    {
        ConsoleOutput.Add(ConsoleInput);
        //myBrush = Brushes.Orange;
        // do your stuff here.
        ConsoleInput = String.Empty;
    }



    private System.Windows.Media.Brush _foregroundColor = System.Windows.Media.Brushes.DarkSeaGreen;

    public System.Windows.Media.Brush ForegroundColor
    {
        get { return _foregroundColor; }
        set
        {
            _foregroundColor = value;
            OnPropertyChanged("ForegroundColor");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class YourType : INotifyPropertyChanged
{
    private string _text;
    public string Text
    {
        get { return _text; }
        set { _text = value; OnPropertyChanged("Text"); }
    }

    private Brush _foreground;
    public Brush Foreground
    {
        get { return _foreground; }
        set { _foreground = value; OnPropertyChanged("Foreground"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
<ScrollViewer Name="Scroller" Margin="0" Background="Black">
    <StackPanel>
        <ItemsControl ItemsSource="{Binding ConsoleOutput, Mode=OneWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Text}" 
                               Foreground="{Binding Foreground}" 
                               Name="SavedBlocks" FontFamily="Consolas"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <TextBox Text="{Binding ConsoleInput, Mode=TwoWay}" Background="Black" Foreground="White" FontFamily="Consolas" Name="InputBlock" BorderBrush="{x:Null}" SelectionBrush="{x:Null}" />
    </StackPanel>
</ScrollViewer>
public void RunCommand()
{
    ConsoleOutput.Add(new YourType { Text = ConsoleInput, Foreground = Brushes.Orange } );
    ConsoleInput = String.Empty;
}