none
ListBox 去除默认边框问题,beta 2中显示正常,RTW中显示不正常 RRS feed

  • 问题

  • beta 2的时候显示正常,现在升级到RTW显示不正常,不知道RTW有啥变化了?

     

    <ListBox x:Name="listBox1" Canvas.Left="250" Canvas.Top="8"  Style="{StaticResource Style1}" >

     

    App.xaml文件中:

     <Application.Resources>
            <Style x:Key="Style1" TargetType="ListBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBox">
                            <Grid>

                                <ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Auto" BorderThickness="0"  HorizontalScrollBarVisibility="Auto">
                                    <ItemsPresenter />
                                </ScrollViewer>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>     
        </Application.Resources>
    </Application>

     

    2008年11月4日 3:31

答案

  • 不知道我理解的对不对.我做了个Demo,是没有边框的.要是你能在我没删除Demo前加我我可以把测试工程发给你.

    QQ 14682233     45166608

     

    UserControl(你上面提到过加一个UserControl就会有边框,所以我加了一个测试的UserControl)

    <UserControl
        x:Class="SilverlightApplication1.SilverlightControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
        <Grid
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
        >
            <TextBlock
                Text="Test"
            />
        </Grid>
    </UserControl>


     

    App.xaml

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 x:Class="SilverlightApplication1.App"
                 >
        <Application.Resources>
            <Style
                x:Key="ListBox"
                TargetType="ListBox"
            >
                <Setter
                    Property="Template"
                >
                    <Setter.Value>
                        <ControlTemplate
                            TargetType="ListBox"
                        >
                            <Grid>
                                <ScrollViewer
                                    x:Name="ScrollViewer"
                                    Padding="{TemplateBinding Padding}"
                                    BorderThickness="0"
                                >
                                    <ItemsPresenter />
                                </ScrollViewer>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
           
            <Style
                x:Key="ListBoxItem"
                TargetType="ListBoxItem"
                xmlns:vsm="clr-namespaceTongue Tiedystem.Windows;assembly=System.Windows"
            >
                <Setter
                    Property="Background"
                    Value="#FFFFFFFF"
                />
                <Setter
                    Property="Template"
                >
                    <Setter.Value>
                        <ControlTemplate
                            TargetType="ListBoxItem"
                        >
                            <Grid
                                Background="{TemplateBinding Background}"
                            >
                                <ContentPresenter
                                    Content="{TemplateBinding Content}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    Margin="5"
                                />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>

    </Application>

     

     

    CStudent类

    public class CStudent
        {
            public string ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }

     

    Page.xaml

    <UserControl
        x:Class="SilverlightApplication1.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlnsTongue Tiedyc="clr-namespaceTongue TiedilverlightApplication1;assembly=SilverlightApplication1"
    >
        <Grid
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
        >
            <ListBox
                x:Name="listboxStudent"
                Width="300"
                Height="200"
                Style="{StaticResource ListBox}"
                ItemContainerStyle="{StaticResource ListBoxItem}"
            >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel
                            Orientation="Horizontal"
                        >
                            <sycTongue TiedilverlightControl1></sycTongue TiedilverlightControl1>
                            <TextBlock
                                Margin="10,0,0,0"
                                Text="{Binding Name}"
                            />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </UserControl>

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication1
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
                InitObject();
            }

            private void InitObject()
            {
                lstStudent = new List<CStudent>();

                lstStudent.Add(new CStudent() { ID = "1", Name = "Name1", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "2", Name = "Name2", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "3", Name = "Name3", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "4", Name = "Name4", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "5", Name = "Name5", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "6", Name = "Name6", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "7", Name = "Name7", Age = 18 });

                listboxStudent.ItemsSource = lstStudent;
            }

            private System.Collections.Generic.List<CStudent> lstStudent;
        }
    }

     

    2008年11月11日 3:26

全部回复

  • 建立不要在Beta2中使用ListBox了,因为到正式版后 ListBox的风格变化很大,你的问题我也遇到过,我通过修改Style将其设置,但到正式版后,我记得风格本来就没有边框,而且数据项中的分割线也要靠自定义风格添加

    2008年11月4日 3:41
  • 正式版中Listbox还是有边框的,而且比Beta 2里的边框还要宽,还要难看

    2008年11月4日 5:09
  • 哪可能是我记错了,反正我记得是很难看,所以我一般都用自已的样式.

    2008年11月4日 5:34
  •  

    那究竟怎么把边框去除呢?
    2008年11月4日 5:35
  • 给你写了个Demo把所有的框都去掉了.

     

    <Application
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="SilverlightApplication1.App"
    >
        <Application.Resources>
            <Style
                x:Key="ListBox"
                TargetType="ListBox"
            >
                <Setter
                    Property="Template"
                >
                    <Setter.Value>
                        <ControlTemplate
                            TargetType="ListBox"
                        >
                            <Grid>
                                <ScrollViewer
                                    x:Name="ScrollViewer"
                                    Padding="{TemplateBinding Padding}"
                                    BorderThickness="0"
                                >
                                    <ItemsPresenter />
                                </ScrollViewer>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>
    </Application>

    2008年11月4日 6:20
  • 我用你的代码测试,显示ok啊,我的是sl2 rtw,可能是其他原因

    2008年11月5日 0:33
    版主
  • 他的ListBox是有边框的,ScrollView没去边框.

     

    2008年11月5日 3:32
  • 我的问题依然存在,不知道怎么解决!

    <UserControl x:Class="Test.ListBox"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
        <Canvas x:Name="LayoutRoot" Background="White">
            <ListBox x:Name="ListBox1" BorderThickness="0" Background="Wheat" Width="200" Height="100" Canvas.Top="0">        
                <ListBoxItem Content="item1" />
                <ListBoxItem Content="item2" />
                <ListBoxItem Content="item3" />
            </ListBox>

            <ListBox x:Name="ListBox2" BorderThickness="0" Width="200" Background="Black" Canvas.Top="120">
            </ListBox>
        </Canvas>
    </UserControl>

     

    后台代码:

     for (int i = 0; i < 6; i++)
                {
                    ListBoxItem1 item = new ListBoxItem1();       //ListBoxItem1 是UseControl,XAML如下

                    ListBox2.Items.Add(item);

                }

     

    UserControl的XAML

    <UserControl x:Class="Test.ListBoxItem1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="180" Height="25">
        <Canvas x:Name="LayoutRoot" Background="White">
            <TextBlock Text="ListBoxItem1" FontSize="16" Canvas.Left="30"></TextBlock>
        </Canvas>
    </UserControl>

     

    效果:

    2008年11月7日 9:09
  • 你把我上面给你给的Style应用上呀,我上面的Style把边框都去掉了.

    2008年11月7日 12:14
  • 我试了一下,Style加上也没有用,只要ListBoxItem是UserControl就会有边框。可我的UserControl里没边框。 红色的是UserControl,

     

     

    外圈透明的就是Listbox的边框

    2008年11月11日 1:39
  • 不知道我理解的对不对.我做了个Demo,是没有边框的.要是你能在我没删除Demo前加我我可以把测试工程发给你.

    QQ 14682233     45166608

     

    UserControl(你上面提到过加一个UserControl就会有边框,所以我加了一个测试的UserControl)

    <UserControl
        x:Class="SilverlightApplication1.SilverlightControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
        <Grid
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
        >
            <TextBlock
                Text="Test"
            />
        </Grid>
    </UserControl>


     

    App.xaml

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 x:Class="SilverlightApplication1.App"
                 >
        <Application.Resources>
            <Style
                x:Key="ListBox"
                TargetType="ListBox"
            >
                <Setter
                    Property="Template"
                >
                    <Setter.Value>
                        <ControlTemplate
                            TargetType="ListBox"
                        >
                            <Grid>
                                <ScrollViewer
                                    x:Name="ScrollViewer"
                                    Padding="{TemplateBinding Padding}"
                                    BorderThickness="0"
                                >
                                    <ItemsPresenter />
                                </ScrollViewer>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
           
            <Style
                x:Key="ListBoxItem"
                TargetType="ListBoxItem"
                xmlns:vsm="clr-namespaceTongue Tiedystem.Windows;assembly=System.Windows"
            >
                <Setter
                    Property="Background"
                    Value="#FFFFFFFF"
                />
                <Setter
                    Property="Template"
                >
                    <Setter.Value>
                        <ControlTemplate
                            TargetType="ListBoxItem"
                        >
                            <Grid
                                Background="{TemplateBinding Background}"
                            >
                                <ContentPresenter
                                    Content="{TemplateBinding Content}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    Margin="5"
                                />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>

    </Application>

     

     

    CStudent类

    public class CStudent
        {
            public string ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }

     

    Page.xaml

    <UserControl
        x:Class="SilverlightApplication1.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlnsTongue Tiedyc="clr-namespaceTongue TiedilverlightApplication1;assembly=SilverlightApplication1"
    >
        <Grid
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
        >
            <ListBox
                x:Name="listboxStudent"
                Width="300"
                Height="200"
                Style="{StaticResource ListBox}"
                ItemContainerStyle="{StaticResource ListBoxItem}"
            >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel
                            Orientation="Horizontal"
                        >
                            <sycTongue TiedilverlightControl1></sycTongue TiedilverlightControl1>
                            <TextBlock
                                Margin="10,0,0,0"
                                Text="{Binding Name}"
                            />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </UserControl>

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication1
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
                InitObject();
            }

            private void InitObject()
            {
                lstStudent = new List<CStudent>();

                lstStudent.Add(new CStudent() { ID = "1", Name = "Name1", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "2", Name = "Name2", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "3", Name = "Name3", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "4", Name = "Name4", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "5", Name = "Name5", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "6", Name = "Name6", Age = 18 });
                lstStudent.Add(new CStudent() { ID = "7", Name = "Name7", Age = 18 });

                listboxStudent.ItemsSource = lstStudent;
            }

            private System.Collections.Generic.List<CStudent> lstStudent;
        }
    }

     

    2008年11月11日 3:26
  •  

    超哥!谢谢你!不好意思!QQ老是被盗,所以我不用QQ的,我只用MSN:guo520jie@126.com
    2008年11月11日 3:36
  •  

    超哥!不知道为什么,效果还是不对。我完全复制你的代码的。恳请你再帮我看一下吧

    2008年11月11日 4:52
  • 哈,我理解错你的意思了,原来你选择选项时也不要效果呀,我把上面的代码修改了,你看一下吧.

    2008年11月11日 5:32
  •  

    终于搞定了。感谢超哥!看来得先学习一下怎么定义Style吧。
    2008年11月11日 6:15