ListBox 或 ItemControl 项目不占用所有可用空间
抱歉,如果之前已经回答过这个问题;我在这里看过类似的帖子,但建议的修复对我不起作用。
我正在开发一个 Silverlight 3 项目,并且有以下内容:
<ListBox ItemsSource="{Binding}"
ItemTemplate="{StaticResource TemplateSelector}"
>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
ListBox 绑定到一个源,该源使用我在 Silverlight 中实现的 DataTemplateSelector 来使用不同的 DataTemplate。
ListBox 似乎忽略了样式属性 HorizontalContentAlignment,即我无法将项目拉伸到 ListBox 的整个宽度。
我的 DataTemplate 如下所示:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DisplayName}" />
<TextBox Text="{Binding Value}" Name="{Binding ID}"/>
</StackPanel>
基本上,我希望这些项目填充 ListBox 控件的所有可用空间,并共享它们,以便左半部分空间由 TextBlock 占据,右半部分由 TextBox 占据。
关于如何做到这一点有什么想法吗?
编辑:@wdavo:这是 TemplateSelector 代码:
// http://www.codeproject.com/Articles/92439/Silverlight-DataTemplateSelector
public abstract class DataTemplateSelector : ContentControl
{
public virtual DataTemplate SelectTemplate(
object item, DependencyObject container)
{
return null;
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
ContentTemplate = SelectTemplate(newContent, this);
}
}
public class MyTemplateSelector : DataTemplateSelector
{
public DataTemplate MyTemplate1 { get; set; }
public DataTemplate MyTemplate2 { get; set; }
public DataTemplate MyTemplate3 { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var element = item;
if (element.GetType() == typeof(MyType1))
return MyTemplate1;
else if (element.GetType() == typeof(MyType2))
return MyTemplate2;
else if (element.GetType() == typeof(MyType3))
return MyTemplate3;
return base.SelectTemplate(item, container);
}
}
和 xaml:
<DataTemplate x:Key="TemplateSelector">
<local:MyTemplateSelector Content="{Binding}">
<local:MyTemplateSelector.MyTemplate1>
<DataTemplate>
<StackPanel >
<TextBlock Text="{Binding DisplayName}" />
<TextBox Text="{Binding Value}" Name="{Binding ID}" />
</StackPanel>
</DataTemplate>
</local:MyTemplateSelector.MyTemplate1>
... and so on
</local:MyTemplateSelector>
</DataTemplate>
Sorry if this has been answered before; I've looked at similar posts here but the suggested fix isn't working for me.
I'm working on a Silverlight 3 project and I have the following:
<ListBox ItemsSource="{Binding}"
ItemTemplate="{StaticResource TemplateSelector}"
>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
The ListBox is bound to a source, which uses different DataTemplates using a DataTemplateSelector that I implemented in Silverlight.
The ListBox seems to ignore the style property HorizontalContentAlignment, i.e. I can't get the items to stretch to the whole width of the ListBox.
My DataTemplates look like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DisplayName}" />
<TextBox Text="{Binding Value}" Name="{Binding ID}"/>
</StackPanel>
Basically, I want the items to fill all the available space of the ListBox control, and share them so that the left half of the space is occupied by the TextBlock, and the right half is occupied by the TextBox.
Any ideas on how to do this?
EDIT: @wdavo: Here's the TemplateSelector code:
// http://www.codeproject.com/Articles/92439/Silverlight-DataTemplateSelector
public abstract class DataTemplateSelector : ContentControl
{
public virtual DataTemplate SelectTemplate(
object item, DependencyObject container)
{
return null;
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
ContentTemplate = SelectTemplate(newContent, this);
}
}
public class MyTemplateSelector : DataTemplateSelector
{
public DataTemplate MyTemplate1 { get; set; }
public DataTemplate MyTemplate2 { get; set; }
public DataTemplate MyTemplate3 { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var element = item;
if (element.GetType() == typeof(MyType1))
return MyTemplate1;
else if (element.GetType() == typeof(MyType2))
return MyTemplate2;
else if (element.GetType() == typeof(MyType3))
return MyTemplate3;
return base.SelectTemplate(item, container);
}
}
and the xaml:
<DataTemplate x:Key="TemplateSelector">
<local:MyTemplateSelector Content="{Binding}">
<local:MyTemplateSelector.MyTemplate1>
<DataTemplate>
<StackPanel >
<TextBlock Text="{Binding DisplayName}" />
<TextBox Text="{Binding Value}" Name="{Binding ID}" />
</StackPanel>
</DataTemplate>
</local:MyTemplateSelector.MyTemplate1>
... and so on
</local:MyTemplateSelector>
</DataTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该只使用
Grid
。 StackPanel 仅使用每个元素所需的最小空间。You should just use a
Grid
. TheStackPanel
is only using the minimum space needed by each element.我猜您需要设置 HorizontalAlignment 和 HorizontalContentAlignment 属性以在 TemplateSelector 中拉伸。
例如,
正如前面提到的,您还应该使用 Grid 而不是 StackPanel
I'm guessing you need to set the HorizontalAlignment and HorizontalContentAlignment properties to stretch within your TemplateSelector.
E.g.
As mentioned you should also you a Grid instead of a StackPanel