इस उत्तर के लिए एक समाधान MVVM का उपयोग कर का वर्णन है।
यह समाधान बहुत अच्छा है यदि आप किसी विंडो में लॉगिंग बॉक्स जोड़ना चाहते हैं, जो प्रत्येक बार एक नया लॉगिंग संदेश जोड़ा जाता है, तो स्वचालित रूप से नीचे स्क्रॉल करता है।
एक बार इन संलग्न गुण जोड़े जाने के बाद, उन्हें कहीं भी पुन: उपयोग किया जा सकता है, इसलिए यह बहुत मॉड्यूलर और पुन: प्रयोज्य सॉफ़्टवेयर बनाता है।, तब
public static class TextBoxClearBehavior
{
public static readonly DependencyProperty TextBoxClearProperty =
DependencyProperty.RegisterAttached(
"TextBoxClear",
typeof(bool),
typeof(TextBoxClearBehavior),
new UIPropertyMetadata(false, OnTextBoxClearPropertyChanged));
public static bool GetTextBoxClear(DependencyObject obj)
{
return (bool)obj.GetValue(TextBoxClearProperty);
}
public static void SetTextBoxClear(DependencyObject obj, bool value)
{
obj.SetValue(TextBoxClearProperty, value);
}
private static void OnTextBoxClearPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs args)
{
if ((bool)args.NewValue == false)
{
return;
}
var textBox = (TextBox)d;
textBox?.Clear();
}
}
यदि आप ':
<TextBox IsReadOnly="True"
Foreground="Gainsboro"
FontSize="13"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
attachedBehaviors:TextBoxApppendBehaviors.AppendText="{Binding LogBoxViewModel.AttachedPropertyAppend}"
attachedBehaviors:TextBoxClearBehavior.TextBoxClear="{Binding LogBoxViewModel.AttachedPropertyClear}"
TextWrapping="Wrap">
इस जुड़ी संपत्ति जोड़ें:: (बॉक्स को साफ़ करने के लिए)
public static class TextBoxApppendBehaviors
{
#region AppendText Attached Property
public static readonly DependencyProperty AppendTextProperty =
DependencyProperty.RegisterAttached(
"AppendText",
typeof (string),
typeof (TextBoxApppendBehaviors),
new UIPropertyMetadata(null, OnAppendTextChanged));
public static string GetAppendText(TextBox textBox)
{
return (string)textBox.GetValue(AppendTextProperty);
}
public static void SetAppendText(
TextBox textBox,
string value)
{
textBox.SetValue(AppendTextProperty, value);
}
private static void OnAppendTextChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs args)
{
if (args.NewValue == null)
{
return;
}
string toAppend = args.NewValue.ToString();
if (toAppend == "")
{
return;
}
TextBox textBox = d as TextBox;
textBox?.AppendText(toAppend);
textBox?.ScrollToEnd();
}
#endregion
}
और यह संलग्न संपत्ति
इस XAML जोड़े एमईएफ जैसे निर्भरता इंजेक्शन ढांचे का उपयोग कर फिर से, आप लॉगिंग-विशिष्ट कोड को अपने स्वयं के व्यू मॉडेल में रख सकते हैं:
public interface ILogBoxViewModel
{
void CmdAppend(string toAppend);
void CmdClear();
bool AttachedPropertyClear { get; set; }
string AttachedPropertyAppend { get; set; }
}
[Export(typeof(ILogBoxViewModel))]
public class LogBoxViewModel : ILogBoxViewModel, INotifyPropertyChanged
{
private readonly ILog _log = LogManager.GetLogger<LogBoxViewModel>();
private bool _attachedPropertyClear;
private string _attachedPropertyAppend;
public void CmdAppend(string toAppend)
{
string toLog = $"{DateTime.Now:HH:mm:ss} - {toAppend}\n";
// Attached properties only fire on a change. This means it will still work if we publish the same message twice.
AttachedPropertyAppend = "";
AttachedPropertyAppend = toLog;
_log.Info($"Appended to log box: {toAppend}.");
}
public void CmdClear()
{
AttachedPropertyClear = false;
AttachedPropertyClear = true;
_log.Info($"Cleared the GUI log box.");
}
public bool AttachedPropertyClear
{
get { return _attachedPropertyClear; }
set { _attachedPropertyClear = value; OnPropertyChanged(); }
}
public string AttachedPropertyAppend
{
get { return _attachedPropertyAppend; }
set { _attachedPropertyAppend = value; OnPropertyChanged(); }
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
यहाँ यह कैसे काम करता है:
- ViewModel संलग्न गुण पाठ बॉक्स नियंत्रित करने के लिए टॉगल करता है।
- चूंकि यह "परिशिष्ट" का उपयोग कर रहा है, यह बिजली तेज है।
- कोई अन्य व्यूमोडेल लॉगिंग व्यूमोडेल पर विधियों को कॉल करके लॉगिंग संदेश उत्पन्न कर सकता है।
- जैसा कि हम टेक्स्टबॉक्स में बनाए गए स्क्रॉलव्यूयर का उपयोग करते हैं, हम प्रत्येक बार एक नया संदेश जोड़ते समय इसे स्वचालित रूप से टेक्स्टबॉक्स के नीचे स्क्रॉल कर सकते हैं।
इस प्रश्न को दोबारा पढ़ने पर, मुझे लगता है कि आप एक बार 'टेक्स्टब्लॉक'' और 'टेक्स्टबॉक्स' का उल्लेख करते हैं। –