其控制会更适合于如下所示的输出? [英] Which Control will be more appropriate to the output shown below?

查看:144
本文介绍了其控制会更适合于如下所示的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是工作好之前,我做我的数据库,我的一些代码修改



前的变化:



输出:

  Tile1 Tile7 ..........瓷砖(NX)
Tile2 Tile8瓷砖(N-X + 1)
Tile3 Tile9 ....
Tile4 Tile10 ....
Tile5 Tile11 ....
Tile6 Tile12瓷砖(N)

表格中的数据库: 1 ------- [主键]
标题|
背景|
图片|
PARENTID * ------- [外键]



XAML:

 <列表框Grid.Row =1×:NAME =乐善堂
的ItemsSource ={结合ChildrenMenus} >

< ListBox.ItemsPanel>
< ItemsPanelTemplate>
< WrapPanel IsItemsHost =真角度=垂直了maxHeight ={绑定的ElementName = LST,路径=的ActualHeight}/>
< / ItemsPanelTemplate>
< /ListBox.ItemsPanel>

< ListBox.Resources>
<风格的TargetType ={X:输入一个ListBoxItem}>
< setter属性=宽度值=250/>
< setter属性=高度值=125/>
< setter属性=保证金VALUE =2.5/>
< setter属性=填充VALUE =2.5/>
< setter属性=背景VALUE ={结合背景,转换器= {StaticResource的stringToBrushConverter}}/>
< setter属性=前景VALUE =白/>
< setter属性=VerticalContentAlignmentVALUE =底/>
< Style.Triggers>
<触发属性=IsSelectedVALUE =真>
< setter属性=前景VALUE ={结合背景,转换器= {StaticResource的stringToBrushConverter}}/>
< /触发>
< /Style.Triggers>
< /样式和GT;
< /ListBox.Resources>

< ListBox.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的方向=横向HEIGHT =125WIDTH =250>
<路径数据={结合图像}VerticalAlignment =中心
弹力=统一填充={绑定路径=前景,的RelativeSource = {的RelativeSource AncestorType = {X:类型ListBoxItem的} }
WIDTH =68HEIGHT =68保证金=10RenderTransformOrigin =0.5,0.5>
< Path.RenderTransform>
<&的TransformGroup GT;
< TransformGroup.Children>
< RotateTransform角=0/>
将; ScaleTransform的ScaleX =1的scaleY =1/>
< /TransformGroup.Children>
< /&的TransformGroup GT;
< /Path.RenderTransform>
< /路径和GT;
< TextBlock的文本={结合标题,转换器= {StaticResource的spaceToNewLineConverter}}VerticalAlignment =评出的
保证金=40,10,10,10字号=24前景= {绑定路径=前景,的RelativeSource = {的RelativeSource AncestorType = {X:输入一个ListBoxItem}}}/>
< / StackPanel的>
< / DataTemplate中>
< /ListBox.ItemTemplate>

< /列表框>



目前



所需的输出:

 文本1文本2文本3 ... .......文本(N)

Tile1 Tile3 Tile7 Tile9 Tile13瓷砖(NX).....
Tile2 Tile4 Tile8 Tile10瓷砖(NX + 1)... ..
Tile5 Tile11 .... .....
Tile6 Tile12 ....瓷砖(N)

变化数据库:





我曾尝试在视图模型和XAML文件的许多变化,现在它弄乱。所以,如果我发布这些代码,然后也不会是任何人都有益。



我希望我有问题正确地提到了一切。



更新



首先我很抱歉。我的互联网连接下跌了整整一天。我刚才已经阅读您的邮件。



现在,我已经得到的东西。我可以Design_Master_MenuItems获得数据库中的数据。见下图:





但还是绑定无法正常工作。我的意思是不被填充我里面的ItemsControl列表框



下面是我当前的XAML:

 <的ItemsControl的ItemsSource ={结合MenuCategories}> 

< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的IsItemsHost =真方向=横向/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>

< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition高度=*/>
< /Grid.RowDefinitions>

< TextBlock的文本={结合标题}字号=30/>

<列表框Grid.Row =1×:NAME =乐善堂
的ItemsSource ={结合Design_Master_TileItems}的DisplayMemberPath =标题>

< /列表框>
< /网格和GT;
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>

下面是myViewModel:

 公共类MainWindowViewModel:ViewModelBase 
{

公共MainWindowViewModel()
{使用(实体DB =新的实体())

{
ParentMenus =新的ObservableCollection< D​​esign_Master_ParentMenus>(在db.Design_Master_ParentMenus从D选择D);

如果(SelectedParent!= NULL)
MenuCategories =新的ObservableCollection<在db.Design_Master_Categories
,其中d.ParentMenuID == SelectedParent.ParentMenuID
(从D组; Design_Master_Categories>选择D);
}
}

私人的ObservableCollection< D​​esign_Master_ParentMenus> _parentMenus;
公众的ObservableCollection< D​​esign_Master_ParentMenus> ParentMenus
{
得到
{
返回_parentMenus;
}

{
_parentMenus =价值;
OnPropertyChanged(ParentMenus);
}
}

私人Design_Master_ParentMenus _selectedParent;
公共Design_Master_ParentMenus SelectedParent
{
得到
{
返回_selectedParent;
}

{
_selectedParent =价值;
OnPropertyChanged(SelectedParent);使用

(实体DB =新的实体())
{
MenuCategories =新的ObservableCollection< D​​esign_Master_Categories>在db.Design_Master_Categories
(从d其中d.ParentMenuID = = SelectedParent.ParentMenuID
选择D);
}
}
}

私人的ObservableCollection< D​​esign_Master_Categories> _menuCategories;
公众的ObservableCollection< D​​esign_Master_Categories> MenuCategories
{
得到
{
返回_menuCategories;
}

{
_menuCategories =价值;
OnPropertyChanged(MenuCategories);
}
}

}



是的,我将不能用于接下来的10个小时。如果你在上面的代码中找到的任何错误,你可以提出意见。感谢一大助力。



UPDATE2



是我现在找到在输出窗口绑定错误:

  System.Windows.Data错误:17:无法获得'Design_Master_TileItem'值(类型'ICollection` 从)''
(输入Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24')1。
BindingExpression:路径= Design_Master_TileItem;
的DataItem ='Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24'
(=的hashCode 28842409);目标元素是'列表框'(名称='');目标属性的ItemsSource'(式
'的IEnumerable')TargetInvocationException:'System.Reflection.TargetInvocationException:属性访问器
'Design_Master_TileItem'的对象
'System.Data.Entity.DynamicProxies.Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A8
8A9017957C24'抛出以下异常:该ObjectContext的实例已经被处置,不能
再用于需要连接的操作---方式> System.ObjectDisposedException:本
ObjectContext的实例已被设置,并且不再能够用于需要一个
连接操作。

System.Windows.Data错误:17:无法获得'Design_Master_TileItem'值从(类型'ICollection`1')''
(输入Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24')。
BindingExpression:路径= Design_Master_TileItem;
的DataItem ='Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24'
(=的hashCode 13006057);目标元素是'列表框'(名称='');目标属性的ItemsSource'(式
'的IEnumerable')TargetInvocationException:'System.Reflection.TargetInvocationException:属性访问器
'Design_Master_TileItem'的对象
'System.Data.Entity.DynamicProxies.Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A8
8A9017957C24'抛出以下异常:该ObjectContext的实例已经被处置,不能
再用于需要连接的操作---方式> System.ObjectDisposedException:本
ObjectContext的实例已被设置,并且不再能够用于需要一个
连接操作。


解决方案

首先,你需要有一个水平的StackPanel作为一个ListView面板模板,让你的大块。



然后,对于每个块,你需要一个头,然后另一个ListView控件,这一次垂直WrapPanel作为面板模板。下面是一个空壳的例子,将需要一些造型和绑定让它看起来正是你想要的方式,但希望它可以让你在正确的轨道上。

 <&的ListView GT; 
< ListView.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向>< / StackPanel的>
< / ItemsPanelTemplate>
< /ListView.ItemsPanel>
< ListView.ItemTemplate>
<&DataTemplate的GT;
<&StackPanel的GT;
<的TextBlock />
<&的ListView GT;
< ListView.ItemPanelTemplate>
< WrapPanel方向=垂直>< / WrapPanel>
< /ListView.ItemPanelTemplate>
< ListView.ItemTemplate>
<&DataTemplate的GT;
<的TextBlock />
< / DataTemplate中>
< /ListView.ItemTemplate>
<&的ListView GT;
< / StackPanel的>
< / DataTemplate中>
< /ListView.ItemTemplate>
<&的ListView GT;



更新:



要使

只是一个选项,确保无论是选择列表框中调用你的财产的制定者。我一般不与的RelativeSource做到这一点,所以这里是一个例子,如果你想尝试它(这里你的窗口/用户控件被命名为根:

 {结合​​ElemantName =根,路径= DataContext.SelectedTileItem}

A转换器是会得到非常复杂做到这一点。这回答有设置你想什么的接受的方式做的,它可能是你想要去的(我会用组名的路线,因为这基本上是什么你正在尝试做的)的方式。


My project was working good before I made some changes in my database and my code.

Before Changes :

Output :

Tile1    Tile7    ..........    Tile(N-x)
Tile2    Tile8                  Tile(N-x+1)
Tile3    Tile9                  ....
Tile4    Tile10                 ....
Tile5    Tile11                 ....
Tile6    Tile12                 Tile(N)

Table in Database: 1------- [Primary Key] Title | Background | Image | ParentID *------- [Foreign Key]

XAML :

<ListBox Grid.Row="1" x:Name="lst"
         ItemsSource="{Binding ChildrenMenus}" >

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="{Binding ElementName=lst, Path=ActualHeight}"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Width" Value="250" />
            <Setter Property="Height" Value="125" />
            <Setter Property="Margin" Value="2.5" />
            <Setter Property="Padding" Value="2.5" />
            <Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="VerticalContentAlignment" Value="Bottom" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="{Binding Background, Converter ={StaticResource stringToBrushConverter}}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Resources>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="125" Width="250">
                <Path Data="{Binding Image}"  VerticalAlignment="Center" 
                      Stretch="Uniform" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                      Width="68" Height="68" Margin="10" RenderTransformOrigin="0.5,0.5">
                    <Path.RenderTransform>
                        <TransformGroup>
                            <TransformGroup.Children>
                                <RotateTransform Angle="0" />
                                <ScaleTransform ScaleX="1" ScaleY="1" />
                            </TransformGroup.Children>
                        </TransformGroup>
                    </Path.RenderTransform>
                </Path>
                <TextBlock Text="{Binding Title, Converter={StaticResource spaceToNewLineConverter}}" VerticalAlignment="Top" 
                           Margin="40,10,10,10" FontSize="24" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

Currently:

Required Output:

Text1     Text2            Text3             ..........     Text(N)

Tile1     Tile3  Tile7     Tile9    Tile13                  Tile(N-x)        .....
Tile2     Tile4  Tile8     Tile10                           Tile(N-x + 1)    .....
          Tile5            Tile11                           ....             .....
          Tile6            Tile12                           ....             Tile(N)

Changes in database:

I have tried many changes in ViewModel and XAML files and now it got messed up. So, if I post those codes then also it will not be useful to anybody.

I hope I have mentioned everything correctly in question.

Update

First of all I am sorry. My internet connection was down for the whole day. I have read your messages just now.

Now, I have got something. I can get data from database in Design_Master_MenuItems. See the Image below:

But still Binding does not work correctly. I mean my ListBoxes inside ItemsControl are not being populated.

Here is my current XAML:

<ItemsControl ItemsSource="{Binding MenuCategories}" >

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding Title}" FontSize="30" />

                <ListBox Grid.Row="1" x:Name="lst"
                         ItemsSource="{Binding Design_Master_TileItems}" DisplayMemberPath="Title">

                </ListBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Here is myViewModel :

public class MainWindowViewModel : ViewModelBase
{

    public MainWindowViewModel()
    {
        using (Entities db = new Entities())
        {
            ParentMenus = new ObservableCollection<Design_Master_ParentMenus>(from d in db.Design_Master_ParentMenus select d);

            if (SelectedParent != null)
                MenuCategories = new ObservableCollection<Design_Master_Categories>(from d in db.Design_Master_Categories
                                                                                  where d.ParentMenuID == SelectedParent.ParentMenuID 
                                                                                  select d);
        }
    }

    private ObservableCollection<Design_Master_ParentMenus> _parentMenus;
    public ObservableCollection<Design_Master_ParentMenus> ParentMenus
    {
        get
        {
            return _parentMenus;
        }
        set
        {
            _parentMenus = value;
            OnPropertyChanged("ParentMenus");
        }
    }

    private Design_Master_ParentMenus _selectedParent;
    public Design_Master_ParentMenus SelectedParent
    {
        get
        {
            return _selectedParent;
        }
        set
        {
            _selectedParent = value;
            OnPropertyChanged("SelectedParent");

            using (Entities db = new Entities())
            {
                MenuCategories = new ObservableCollection<Design_Master_Categories>(from d in db.Design_Master_Categories
                                                                                  where d.ParentMenuID == SelectedParent.ParentMenuID
                                                                                  select d);
            }
        }
    }

    private ObservableCollection<Design_Master_Categories> _menuCategories;
    public ObservableCollection<Design_Master_Categories> MenuCategories
    {
        get
        {
            return _menuCategories;
        }
        set
        {
            _menuCategories = value;
            OnPropertyChanged("MenuCategories");
        }
    }

}

Yes, and I will not be available for next 10 hours. If you find any mistake in the above code you may comment. Thanks for a big helping hand.

Update2

Yes now I find the binding error in Output window:

System.Windows.Data Error: 17 : Cannot get 'Design_Master_TileItem' value (type 'ICollection`1') from '' 
(type 'Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24').     
BindingExpression:Path=Design_Master_TileItem; 
DataItem='Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24' 
(HashCode=28842409); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 
'IEnumerable') TargetInvocationException:'System.Reflection.TargetInvocationException: Property accessor 
'Design_Master_TileItem' on object
'System.Data.Entity.DynamicProxies.Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A8
8A9017957C24' threw the following exception:'The ObjectContext instance has been disposed and can no     
longer be used for operations that require a connection.' ---> System.ObjectDisposedException: The 
ObjectContext instance has been disposed and can no longer be used for operations that require a 
connection.

System.Windows.Data Error: 17 : Cannot get 'Design_Master_TileItem' value (type 'ICollection`1') from '' 
(type 'Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24'). 
BindingExpression:Path=Design_Master_TileItem; 
DataItem='Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A88A9017957C24' 
(HashCode=13006057); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 
'IEnumerable') TargetInvocationException:'System.Reflection.TargetInvocationException: Property accessor 
'Design_Master_TileItem' on object 
'System.Data.Entity.DynamicProxies.Design_Master_Catego_79D2EFE4D31EC6575261E40C340C9D078D37C022F94C70A5F8A8    
8A9017957C24' threw the following exception:'The ObjectContext instance has been disposed and can no 
longer be used for operations that require a connection.' ---> System.ObjectDisposedException: The     
ObjectContext instance has been disposed and can no longer be used for operations that require a       
connection.

解决方案

First you want a ListView with a horizontal StackPanel as the panel template to get your "big" blocks.

Then, for each block, you'll need a "header" and then another ListView, this time with a vertical WrapPanel as the panel template. Below is a "shell" example that would need some styling and bindings to get it to look exactly the way you want, but hopefully it gets you on the right track.

<ListView>
   <ListView.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel Orientation="Horizontal"></StackPanel>
       </ItemsPanelTemplate>
   </ListView.ItemsPanel>
   <ListView.ItemTemplate>
       <DataTemplate>
           <StackPanel>
               <TextBlock/>
               <ListView>
                   <ListView.ItemPanelTemplate>
                       <WrapPanel Orientation="Vertical"></WrapPanel>
                   </ListView.ItemPanelTemplate>
                   <ListView.ItemTemplate>
                       <DataTemplate>
                           <TextBlock/>
                       </DataTemplate>
                   </ListView.ItemTemplate>
               <ListView>
           </StackPanel>
       </DataTemplate>
   </ListView.ItemTemplate>
<ListView>

Update:

To have only "one selection", make sure that selecting either list box calls the setter on your property. I normally don't do this with RelativeSource, so here is an example if you want to try it (where your window/user control is named "Root":

"{Binding ElemantName=Root, Path=DataContext.SelectedTileItem}" 

A converter is going to get really complicated to do this. This answer has an accepted way of setting up what you are trying to do, and it is probably the way you want to go (I would use the group name route, since that is basically what you are trying to do).

这篇关于其控制会更适合于如下所示的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆