C# 文本块滚动在WPF中不工作
我想让我的C# 文本块滚动在WPF中不工作,c#,wpf,textblock,C#,Wpf,Textblock,我想让我的TextBlock可滚动,但我无法让它工作。问题可能出在堆叠面板中 下面是代码: <Grid Margin="3"> <Grid.RowDefinitions> <RowDefinition Height="152*" /> <RowDefinition Height="86*" /> <RowDefinition Height="67*" /
TextBlock
可滚动,但我无法让它工作。问题可能出在堆叠面板中
下面是代码:
<Grid Margin="3">
<Grid.RowDefinitions>
<RowDefinition Height="152*" />
<RowDefinition Height="86*" />
<RowDefinition Height="67*" />
</Grid.RowDefinitions>
<ListBox x:Name="lstItems" Margin="3,6,3,0" ItemsSource="{Binding Source={StaticResource rssData}}"
SelectedIndex="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" />
<TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" Grid.RowSpan="2">
<StackPanel.ScrollOwner>
<ScrollViewer />
</StackPanel.ScrollOwner>
<TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" />
<TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" />
<ScrollViewer>
<TextBlock ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="3" FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Height="216" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" />
</ScrollViewer>
</StackPanel>
</Grid>
将此代码粘贴到您的项目中,因为这是我阅读信息的rss链接。看看你会得到什么以下是我必须做的,希望你也能做到。首先,我必须将
ScrollViewer
放置在StackPanel
周围,然后我必须从文本块中删除高度
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical">
<TextBlock Text="Test" />
<TextBlock x:Name="test" Margin="3" FontStyle="Italic" VerticalAlignment="Stretch"
TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" />
</StackPanel>
</ScrollViewer>
编辑
需要指出的是…我修改了你的网格行高度。*用于表示填充,数字并不表示像素。因此,您的hd=ad行为不像带有resize的min。Essentiall 187*并不意味着贪婪的187像素,其中空间至少为187像素,但会根据需要增长。如上所述,将三行高度设置为*只会使每个行的高度为父行高度的三分之一。如果希望第二行的大小是其他行的两倍,请将其他行设置为*并将中间行设置为2*。因为我看不到你的屏幕,你可以根据需要调整。您可能还对使用Auto和根据内容调整大小感兴趣
这是一个为我工作的截图:
我没有看到任何奇怪的东西。TextBlock本身将内容设置为只读,因此我将在StackPanel之外尝试,看看内容是否可滚动。我尝试过,但仍然没有任何结果…尝试删除StackPanel.ScrollOwnerI我刚刚创建了一个快速的wpf项目,无法使其正常工作。我会继续玩的,好的。只是尝试了你说的,但它不起作用。问题是我对wpf完全是新手。你能把你的代码和我上面的代码结合起来吗?请。更新的代码在编辑部分。您可能还需要调整一下。您好,文本块中似乎没有显示任何内容。我不知道位于网格中的StackPanel上的行跨度为2。行1。我更新了代码。仍然没有,我根本看不到文本块区域。其他东西都在加载,只有那部分没有加载
<Window x:Class="RssDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RSS Demo" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="576" Width="521">
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE2E2E2" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Window.Background>
<Window.Resources>
<XmlDataProvider x:Key="rssData" XPath="//item" Source="http://www.hak.hr/rss/stanje/hr.xml" />
</Window.Resources>
<Grid Margin="3">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox x:Name="lstItems" Margin="3,6,3,0" ItemsSource="{Binding Source={StaticResource rssData}}"
SelectedIndex="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" />
<TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}">
<TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" />
<TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" />
</StackPanel>
<ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<TextBlock ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="3"
FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False"
Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" />
</ScrollViewer>
</Grid>
</Window>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical">
<TextBlock Text="Test" />
<TextBlock x:Name="test" Margin="3" FontStyle="Italic" VerticalAlignment="Stretch"
TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" />
</StackPanel>
</ScrollViewer>
<Grid Margin="3">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox x:Name="lstItems" Margin="3,6,3,0" ItemsSource="{Binding Source={StaticResource rssData}}"
SelectedIndex="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" />
<TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}">
<TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" />
<TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" />
</StackPanel>
<ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<TextBlock Margin="3"
FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False"
Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" />
</ScrollViewer>
</Grid>