你们很亲密。
首先,我将HorizontalAlignment="Left" 添加到ScrollViewer,以防止它拉伸到其父级的整个宽度。
其次,我删除了ScaleTransform,它将矩形缩小到水平大小为零。
<StackPanel VerticalAlignment="Center" >
<ScrollViewer
Margin="0"
x:Name="PART_ContentHost"
HorizontalAlignment="Left"
/>
<Rectangle
x:Name="Rect1"
Width="{Binding ActualWidth, ElementName=PART_ContentHost}"
Height="2"
Fill="{TemplateBinding Foreground}" Opacity="0.8"
HorizontalAlignment="Left"
>
</Rectangle>
</StackPanel>
现在您遇到了另一个问题:用户必须在ScrollViewer 内单击以使其获得焦点,这样他才能开始输入,而滚动查看器是挤在控件左侧的一个小东西。我正在寻找一些方法来做到这一点;等待更新。
更新
如何在不对 MVVM 犯任何罪的情况下处理鼠标按下的焦点:
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace MyRandomNamespace
{
public static class Extensions
{
#region Extensions.MouseDownFocusRecipient Attached Property
// Attached property. See XAML example below for usage.
// On mouse down, sets focus on bound target control.
public static UIElement GetMouseDownFocusRecipient(UIElement obj)
{
return (UIElement)obj.GetValue(MouseDownFocusRecipientProperty);
}
public static void SetMouseDownFocusRecipient(UIElement obj, UIElement value)
{
obj.SetValue(MouseDownFocusRecipientProperty, value);
}
public static readonly DependencyProperty MouseDownFocusRecipientProperty =
DependencyProperty.RegisterAttached("MouseDownFocusRecipient", typeof(UIElement), typeof(Extensions),
new PropertyMetadata(null, MouseDownFocusRecipient_PropertyChanged));
private static void MouseDownFocusRecipient_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as UIElement;
// Target must have some kind of background color or it will ignore mouse events.
// We can't do this object-orientedly because multiple Background dependency
// properties are defined in multiple control classes.
var bkgDepProp = DependencyPropertyDescriptor.FromName("Background", target.GetType(), target.GetType(), true);
if (bkgDepProp != null && bkgDepProp.GetValue(target) == null)
{
bkgDepProp.SetValue(target, System.Windows.Media.Brushes.Transparent);
}
//target.IsHitTestVisible = true;
target.PreviewMouseDown -= Target_PreviewMouseDown;
target.PreviewMouseDown += Target_PreviewMouseDown;
}
private static void Target_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var target = (UIElement)sender;
var otherControl = GetMouseDownFocusRecipient(target);
if (otherControl != null)
{
Keyboard.Focus(otherControl);
}
}
#endregion Extensions.MouseDownFocusRecipient Attached Property
}
}
XAML:
<ControlTemplate TargetType="{x:Type TextBoxBase}" x:Key="TextBoxTemplate">
<Border
Name="Border"
Padding="2"
Background="{DynamicResource White#}"
local:Extensions.MouseDownFocusRecipient="{Binding ElementName=PART_ContentHost}"
>
<StackPanel
VerticalAlignment="Center"
MouseDown="StackPanel_MouseDown"
>
<ScrollViewer
Margin="0"
x:Name="PART_ContentHost"
HorizontalAlignment="Left"
/>
<Rectangle
x:Name="Rect1"
Width="{Binding ActualWidth, ElementName=PART_ContentHost}"
Height="8"
Fill="{TemplateBinding Foreground}" Opacity="0.8"
IsHitTestVisible="False"
HorizontalAlignment="Left"
/>
</StackPanel>
</Border>
</ControlTemplate>