问题描述
由于一些奇怪的原因,它是嵌入在滚动查看器我的WPF文本框的形象后,我加入了大量的文字给它得到下面突然中断等。有一些限制或东西是达到了,我可以做更大的东西?
我没有收到任何错误消息。
下面是相关的XAML:
<电网X:名称=的ContentPanelGrid.Row =1保证金= 12,0,12,0>
<按钮X:NAME =Slice_ButtonCONTENT =切片的HorizontalAlignment =左保证金=106,0,0,0VerticalAlignment =评出的点击=Slice_Button_ClickHEIGHT =87背景=#FF0D5B1E/>
<按钮X:NAME =CancelButtonCONTENT =取消的HorizontalAlignment =左保证金=232,0,0,0VerticalAlignment =评出的HEIGHT =87点击=Button_Click_1背景=#FFC70E0E/>
<的ScrollViewer X:NAME =滚轮的HorizontalAlignment =左HEIGHT =505保证金=10,92,0,0VerticalAlignment =评出的WIDTH =436>
<文本框X:NAME =发件箱TextWrapping =自动换行文本=输出这里会显示IsReadOnly =真保证金=2/>
< /&的ScrollViewer GT;
< /网格和GT;
下面是C#我用它来添加文本:
main.DispatchInvoke(()=>
{
main.OutBox.Text + = newText;
main.Scroller.ScrollToVerticalOffset (main.Scroller.ScrollableHeight);
main.Scroller.UpdateLayout();
});
第二次更新:
好了,所以我决定让Windows Phone的工具包和尝试了这一点。
文本框
刚才提到的OP不会滚动。因此,我决定看看它的默认控件模板
这是剥夺了控件模板
从vs2012随着Windows Phone 8 SDK的文本框
:
<的ControlTemplate的TargetType =文本框>
<网格背景=透明>
<! - 的VisualState组为ABT 100线 - >
< BORDER X:NAME =MainBorder
保证金={StaticResource的PhoneTouchTargetOverhang}
背景={TemplateBinding背景}
BorderBrush ={TemplateBinding BorderBrush}
了borderThickness ={TemplateBinding了borderThickness}/>
< BORDER X:NAME =ReadonlyBorder
保证金={StaticResource的PhoneTouchTargetOverhang}
背景=透明
BorderBrush ={StaticResource的PhoneDisabledBrush}
了borderThickness ={TemplateBinding了borderThickness}
能见度=坍塌/>
< BORDER保证金={StaticResource的PhoneTouchTargetOverhang}
=背景透明
BorderBrush =透明
了borderThickness ={TemplateBinding了borderThickness}>
< ContentControl中X:NAME =ContentElement的
保证金={StaticResource的PhoneTextBoxInnerMargin}
HorizontalContentAlignment =拉伸
VerticalContentAlignment =拉伸
了borderThickness = 0
填充={TemplateBinding填充}/>
< /边框>
< /网格和GT;
< /控件模板>
没有的ScrollViewer
只是一个 ContentControl中
。下面是vs2012一个桌面应用程序文本框
<控件模板的TargetType ={X:TextBox类型}>
< BORDER X:名称=边境
背景={TemplateBinding背景}
BorderBrush ={TemplateBinding BorderBrush}
了borderThickness ={TemplateBinding了borderThickness}
SnapsToDevicePixels =真>
<的ScrollViewer X:NAME =PART_ContentHost
调焦=FALSE
HorizontalScrollBarVisibility =隐藏
VerticalScrollBarVisibility =隐藏/>
< /边框>
同时通过复制手机模板,我的桌面应用程序,同样的行为和snoop,验证了这一点。
不知道生根粉推理为什么它不具有的ScrollViewer
,但加入一个在控件模板
不爽的问题。
解决方法:
全部风格
为文本框
与的ScrollViewer
(新增App.xaml中内 - > < Application.Resources>< /Application.Resources>
<风格X:键=TextBoxStyle1
的TargetType =文本框>
< setter属性=的FontFamily
值={StaticResource的PhoneFontFamilyNormal}/>
< setter属性=字号
值={StaticResource的PhoneFontSizeMediumLarge}/>
< setter属性=背景
值={StaticResource的PhoneTextBoxBrush}/>
< setter属性=前景
值={StaticResource的PhoneTextBoxForegroundBrush}/>
< setter属性=BorderBrush
值={StaticResource的PhoneTextBoxBrush}/>
< setter属性=selectionBackground已
值={StaticResource的PhoneAccentBrush}/>
< setter属性=SelectionForeground
值={StaticResource的PhoneTextBoxSelectionForegroundBrush}/>
< setter属性=了borderThickness,
值={StaticResource的PhoneBorderThickness}/>
< setter属性=填充
值=2/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType =文本框>
<网格背景=透明>
< VisualStateManager.VisualStateGroups>
< VisualStateGroup X:NAME =CommonStates>
<的VisualState X:名称=正常/>
<的VisualState X:NAME =鼠标悬停/>
<的VisualState X:名称=已禁用>
<情节提要>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =MainBorder
Storyboard.TargetProperty =背景>
< DiscreteObjectKeyFrame KeyTime =0
值=透明/>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =MainBorder
Storyboard.TargetProperty =BorderBrush>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneDisabledBrush}/>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =ContentElement的
Storyboard.TargetProperty =前景>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneDisabledBrush}/>
< / ObjectAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
<的VisualState X:NAME =只读>
<情节提要>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =MainBorder
Storyboard.TargetProperty =能见度>
将; DiscreteObjectKeyFrame KeyTime =0>
< DiscreteObjectKeyFrame.Value>
<能见度和GT;倒塌< /能见度和GT;
< /DiscreteObjectKeyFrame.Value>
< / DiscreteObjectKeyFrame>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =ReadonlyBorder
Storyboard.TargetProperty =能见度>
将; DiscreteObjectKeyFrame KeyTime =0>
< DiscreteObjectKeyFrame.Value>
<能见度和GT;可见的LT; /能见度和GT;
< /DiscreteObjectKeyFrame.Value>
< / DiscreteObjectKeyFrame>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =ReadonlyBorder
Storyboard.TargetProperty =背景>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneTextBoxBrush}/>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =ReadonlyBorder
Storyboard.TargetProperty =BorderBrush>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneTextBoxBrush}/>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =ContentElement的
Storyboard.TargetProperty =前景>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneTextBoxReadOnlyBrush}/>
< / ObjectAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
< / VisualStateGroup>
< VisualStateGroup X:NAME =FocusStates>
<的VisualState X:NAME =聚焦>
<情节提要>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =MainBorder
Storyboard.TargetProperty =背景>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneTextBoxEditBackgroundBrush}/>
< / ObjectAnimationUsingKeyFrames>
< ObjectAnimationUsingKeyFrames Storyboard.TargetName =MainBorder
Storyboard.TargetProperty =BorderBrush>
< DiscreteObjectKeyFrame KeyTime =0
值={StaticResource的PhoneTextBoxEditBorderBrush}/>
< / ObjectAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
<的VisualState X:NAME =未聚焦/>
< / VisualStateGroup>
< /VisualStateManager.VisualStateGroups>
< BORDER X:NAME =MainBorder
保证金={StaticResource的PhoneTouchTargetOverhang}
背景={TemplateBinding背景}
BorderBrush ={TemplateBinding BorderBrush}
了borderThickness ={TemplateBinding了borderThickness}/>
< BORDER X:NAME =ReadonlyBorder
保证金={StaticResource的PhoneTouchTargetOverhang}
背景=透明
BorderBrush ={StaticResource的PhoneDisabledBrush}
了borderThickness ={TemplateBinding了borderThickness}
能见度=坍塌/>
< BORDER保证金={StaticResource的PhoneTouchTargetOverhang}
=背景透明
BorderBrush =透明
了borderThickness ={TemplateBinding了borderThickness}>
<&的ScrollViewer GT;
< ContentControl中X:NAME =ContentElement的
保证金={StaticResource的PhoneTextBoxInnerMargin}
HorizontalContentAlignment =拉伸
VerticalContentAlignment =拉伸
了borderThickness = 0
填充={TemplateBinding填充}/>
< /&的ScrollViewer GT;
< /边框>
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
用法:
<电网X:名称=的ContentPanel
Grid.Row =1
保证金=12,0,12,0>
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition高度=*/>
< /Grid.RowDefinitions>
< StackPanel的Grid.Row =0
的HorizontalAlignment =中心
方向=横向>
<按钮X:NAME =Slice_Button
保证金=10 0
背景=#FF0D5B1E
含量=切片/>
<按钮X:NAME =CancelButton
保证金=10 0
背景=#FFC70E0E
含量=取消/>
< / StackPanel的>
<文本框X:NAME =发件箱
Grid.Row =1
保证金=10
IsReadOnly =真
风格= {StaticResource的TextBoxStyle1}
文本=输出将在此处显示为
TextWrapping =自动换行/>
< /网格和GT;
请注意只是包裹文本框
在的ScrollViewer
滚动不仅仅是其中的内容可能不能从一个UX POV非常有吸引力的整个控制
下载链接到示例项目通过以上修正:
For some strange reason my WPF textbox which is embedded in a scroll viewer get abruptly cut off like in the image below after I have added a lot of text to it. Is there some limit or something that is reached that I can make larger or something?
I do not receive any error messages.
Here is the relevant Xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button x:Name="Slice_Button" Content="Slice" HorizontalAlignment="Left" Margin="106,0,0,0" VerticalAlignment="Top" Click="Slice_Button_Click" Height="87" Background="#FF0D5B1E"/>
<Button x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="232,0,0,0" VerticalAlignment="Top" Height="87" Click="Button_Click_1" Background="#FFC70E0E"/>
<ScrollViewer x:Name="Scroller" HorizontalAlignment="Left" Height="505" Margin="10,92,0,0" VerticalAlignment="Top" Width="436">
<TextBox x:Name="OutBox" TextWrapping="Wrap" Text="Output will be displayed here" IsReadOnly="True" Margin="2"/>
</ScrollViewer>
</Grid>
Here is the C# I use to add the text:
main.DispatchInvoke(() =>
{
main.OutBox.Text += newText;
main.Scroller.ScrollToVerticalOffset(main.Scroller.ScrollableHeight);
main.Scroller.UpdateLayout();
});
Second Update:
Ok so I decided to get the windows phone kit and try this out.
TextBox
as the OP mentioned just wouldn't scroll. Hence I decided to look at it's default ControlTemplate
This is the stripped out ControlTemplate
from vs2012 with windows phone 8 sdk for TextBox
:
<ControlTemplate TargetType="TextBox">
<Grid Background="Transparent">
<!-- VisualState Groups for abt 100 lines -->
<Border x:Name="MainBorder"
Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<Border x:Name="ReadonlyBorder"
Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="Transparent"
BorderBrush="{StaticResource PhoneDisabledBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Visibility="Collapsed" />
<Border Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentControl x:Name="ContentElement"
Margin="{StaticResource PhoneTextBoxInnerMargin}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
BorderThickness="0"
Padding="{TemplateBinding Padding}" />
</Border>
</Grid>
</ControlTemplate>
No ScrollViewer
just a ContentControl
. The following is from vs2012 for a desktop app TextBox
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost"
Focusable="False"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden" />
</Border>
Also verified this with Snoop by copying the phone template to my desktop app and same behavior.
Not sure abt the reasoning for why it doesn't have a ScrollViewer
, but adding one in the ControlTemplate
sorts the issue out.
Solution:
Full Style
for TextBox
with ScrollViewer
(Added within "App.xaml" -> <Application.Resources></Application.Resources>
<Style x:Key="TextBoxStyle1"
TargetType="TextBox">
<Setter Property="FontFamily"
Value="{StaticResource PhoneFontFamilyNormal}" />
<Setter Property="FontSize"
Value="{StaticResource PhoneFontSizeMediumLarge}" />
<Setter Property="Background"
Value="{StaticResource PhoneTextBoxBrush}" />
<Setter Property="Foreground"
Value="{StaticResource PhoneTextBoxForegroundBrush}" />
<Setter Property="BorderBrush"
Value="{StaticResource PhoneTextBoxBrush}" />
<Setter Property="SelectionBackground"
Value="{StaticResource PhoneAccentBrush}" />
<Setter Property="SelectionForeground"
Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}" />
<Setter Property="BorderThickness"
Value="{StaticResource PhoneBorderThickness}" />
<Setter Property="Padding"
Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="Transparent" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneDisabledBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneDisabledBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="ReadOnly">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ReadonlyBorder"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ReadonlyBorder"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneTextBoxBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ReadonlyBorder"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneTextBoxBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneTextBoxReadOnlyBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneTextBoxEditBackgroundBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource PhoneTextBoxEditBorderBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="MainBorder"
Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<Border x:Name="ReadonlyBorder"
Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="Transparent"
BorderBrush="{StaticResource PhoneDisabledBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Visibility="Collapsed" />
<Border Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer>
<ContentControl x:Name="ContentElement"
Margin="{StaticResource PhoneTextBoxInnerMargin}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
BorderThickness="0"
Padding="{TemplateBinding Padding}" />
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
usage:
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button x:Name="Slice_Button"
Margin="10 0"
Background="#FF0D5B1E"
Content="Slice" />
<Button x:Name="CancelButton"
Margin="10 0"
Background="#FFC70E0E"
Content="Cancel" />
</StackPanel>
<TextBox x:Name="OutBox"
Grid.Row="1"
Margin="10"
IsReadOnly="True"
Style="{StaticResource TextBoxStyle1}"
Text="Output will be displayed here"
TextWrapping="Wrap" />
</Grid>
Do note just wrapping the TextBox
in a ScrollViewer
scroll's the entire control than just the content within it which might not be very appealing from a UX POV
Download Link to Sample Project With above fix: DropBox
这篇关于WPF文本框被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!