Windows phone 7 自定义列表框控件不允许滚动
我在ListBox顶部构建了一个Logger控件,我很难让Logger工作(即ListBox滚动)。以下是我正在使用的代码: 资源字典中的样式定义:Windows phone 7 自定义列表框控件不允许滚动,windows-phone-7,listbox,Windows Phone 7,Listbox,我在ListBox顶部构建了一个Logger控件,我很难让Logger工作(即ListBox滚动)。以下是我正在使用的代码: 资源字典中的样式定义: <Style TargetType="local:Logger"> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <Border x:N
<Style TargetType="local:Logger">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border x:Name="LayoutRoot">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Head}" Margin="4"/>
<TextBlock Grid.Column="1" Text="{Binding Body}" Margin="4" TextWrapping="Wrap"/>
</Grid>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
C#定义记录器的对应项:
public class LoggerMessage
{
public string Head { get; set; }
public string Body { get; set; }
}
public class Logger : ListBox
{
private ObservableCollection<LoggerMessage> _log;
public Logger() : base()
{
DefaultStyleKey = typeof(Logger);
_log = new ObservableCollection<LoggerMessage>();
for (int i = 0; i < 30; i++)
{
_log.Add(new LoggerMessage() { Head = "Msg: " + i, Body = "Some really long text which hopefully wraps around." });
}
this.ItemsSource = _log;
}
public void log(LoggerMessage item)
{
_log.Add(item);
}
}
公共类日志消息
{
公共字符串头{get;set;}
公共字符串体{get;set;}
}
公共类记录器:ListBox
{
私有可观测收集日志;
公共记录器():base()
{
DefaultStyleKey=typeof(记录器);
_log=新的ObservableCollection();
对于(int i=0;i<30;i++)
{
_Add(新的LoggerMessage(){Head=“Msg:+i,Body=”一些非常长的文本,希望能环绕它。”});
}
this.ItemsSource=\u log;
}
公共作废日志(日志消息项)
{
_日志。添加(项目);
}
}
经过一番研究和反复试验,我找到了解决问题的样式模板(见下文),但我无法理解为什么它在版本1中不起作用。为什么我必须在ControlTemplate中显式“重新定义”ScrollViewer
<Style TargetType="local:Logger">
<!-- Begin - Redef of ControlTemplate -->
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:Logger">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- End - Redef of ControlTemplate -->
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Head}" Margin="4"/>
<TextBlock Grid.Column="1" Text="{Binding Body}" Margin="4" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
提前谢谢你的解释
在仔细阅读MSDN文档()之后,将样式应用于控件似乎是一种完全的覆盖。看起来基类的样式并不像我假设的那样被继承。文件中没有明确说明,但上面的实验分类导致了这种理解。FYI,我遇到了同样的问题。似乎仅仅删除DefaultStyleKey的设置就可以解决这个问题。我想我还没有任何副作用。这有可能不再需要了吗?