Login Register
venerdì 10 febbraio 2012
 
Forums
I Forum di WPF Tips & Tricks
Consiglio su idea di binding
Last Post 08 gen 2010 04:42 by Andrea Spaggiari. 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Andrea Spaggiari
Basic Member
Basic Member
Posts:19
Avatar

--
07 gen 2010 07:31  
Ciao Ragazzi,

sto muovendo i primi passi con WPF ed ADO.NET ENTITY DATA MODEL in VB 2008 e vorrei sapere se è fattibile un'idea che mi è venuta in mente.

DESCRIZIONE

Sto utilizzando per le prove Northwind;
vorrei, nella WINDOW1 elencare tutti i prodotti, raggruppati per categoria, magari separati da un BORDER.

COSA HO FATTO FINO AD ORA

Ho creato un DataTemplate con questo XAML:

<Window x:Class="Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="502" Width="630">

<Window.Resources>
<DataTemplate x:Key="dtProva">
<ListBox>
<Label Name="lblCategoria" Background="Beige" Content="{Binding Path=CategoryID}" />
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Label Name="lblProdotto" Background="Azure" Content="{Binding Path=ProductName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ListBox>
</DataTemplate>
</Window.Resources>
<WrapPanel Name="wrap1" Orientation="Vertical" />
</Window>

Nel codice ho scritto:

Imports System.Collections.ObjectModel

Class Window1

Private db As New NORTHWNDEntities

Private ListaProdotti As New ObservableCollection(Of Products)

Private Sub ProdottiPerCategoria()

For Each categoria In db.Categories

Dim prodotti = From p In db.Products _
Where p.Categories.CategoryID = categoria.CategoryID _
Select p

Dim nuovo As New ListBox
nuovo.ItemTemplate = New DataTemplate("{StaticResourcedtProvaDataTemplate}")

Next


End Sub

Public Sub New()

' Chiamata richiesta da Progettazione Windows Form.
InitializeComponent()

' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
ProdottiPerCategoria()

End Sub

End Class

PROBLEMA

Ovviamente non funziona... perchè?

Almeno sono indirizzato bene?

Vorrei che le varie LISTBOX si "wrappassero" nel WRAPPANEL...


Spero di essere stato chiaro e di aver inserito il thread nella sezione giusta...

Grazie
Andrea Spaggiari
Basic Member
Basic Member
Posts:19
Avatar

--
07 gen 2010 09:20  
Allora, ho cambiato le origini... ora sto cercando gli ordini dei vari clienti.... e sono a buon punto!

Il codice che ho scritto è:

Private Sub OrdiniPerCliente()

For Each c In db.Customers

Dim ordine = From o In db.Orders Where pr.Customers.CustomerID = o.CustomerID Select o

Dim stk As New StackPanel
With stk
.Orientation = Orientation.Vertical
End With

Dim chk As New CheckBox
With chk
.Content = c.CompanyName
End With

Dim lst As New ListBox
With lst
.ItemsSource = ordine
.DisplayMemberPath = "OrderID"
End With

Me.wrap1.Children.Add(stk)
stk.Children.Add(chk)
stk.Children.Add(lst)

Next
End Sub

E tutto questo, come dicevo, funziona, ma.... come inserisco i vari stili?
Andrea Spaggiari
Basic Member
Basic Member
Posts:19
Avatar

--
08 gen 2010 12:30  
Con questo codice ho uno stile approssimativo...



Dim clienti = From cust In db.Customers Select cust
For Each c In clienti

Dim stk As New WrapPanel With {.Orientation = Orientation.Vertical}
Wrap1.Children.Add(stk)

Dim lbl As New Label With {.Background = New SolidColorBrush(Colors.Beige), .Content = c.CompanyName, .Margin = New System.Windows.Thickness(5)}
Dim lst As New ListBox
stk.Children.Add(lbl)
stk.Children.Add(lst)

Dim ordini = From ord In db.Orders Where ord.Customers.CustomerID = c.CustomerID Select ord

For Each o In ordini
Dim stk2 As New WrapPanel With {.Background = New SolidColorBrush(Colors.Brown)}
lst.Items.Add(stk2)
Dim but As New Label With {.Content = o.OrderID, .Margin = New System.Windows.Thickness(5), .Padding = New System.Windows.Thickness(3)}
Dim but2 As New Label With {.Content = o.OrderDate, .Margin = New System.Windows.Thickness(5), .Padding = New System.Windows.Thickness(3)}
stk2.Children.Add(but)
stk2.Children.Add(but2)
Next

Next



... ma se riuscissi ad inserirvi da codice qualcosa di preparato vi xaml...
... inoltre, come faccio, ad esempio, a dire al programma, dopo aver premuto un pulsante, di converitre gli prdini eventualmente selezionati, in Orders, in modo tale da poterli elaborare con le ObservableCollection?
Andrea Spaggiari
Basic Member
Basic Member
Posts:19
Avatar

--
08 gen 2010 01:32  
Aggiornamento...

Provando con questo...


<Window.Resources>
<Style x:Key="stileLabel" TargetType="Label">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF04FF41" Offset="0"/>
<GradientStop Color="#FFA1F4B5" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
</Style>
</Window.Resources>


ed inserendo ad esempio in una Label...


Dim lbl As New Label With {.Style = CType(FindResource("stileLabel"), Style), .Content = c.CompanyName, .Margin = New System.Windows.Thickness(5)}


...sono riuscito ad inserire uno stile dove volevo io.. (ma che fatica ragazzi.... però è bellissimo quando si riesce in una cosa mai fatta prima, provando e riprovando e riprovando...)... il procedimento corretto?
Mi rimane l'associazione ai vari eventi...
Andrea Spaggiari
Basic Member
Basic Member
Posts:19
Avatar

--
08 gen 2010 04:42  
Ho provato un'altra strada...


<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="850" Width="1400">
<Grid Background="Beige">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<ListBox Grid.Row="1" Name="ListBox1" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="2" CornerRadius="4,4,4,4" BorderThickness="1" BorderBrush="Black" Width="150">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=OrderID}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Label Content="{Binding Path=OrderDate}" />
<Label Content="{Binding Path=ShipCountry}" />
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>


...e nel codice...


Imports System.Collections.ObjectModel

Class Window1

Private db As New NORTHWNDEntities

Private Function Lista() As ObservableCollection(Of Orders)

Return New ObservableCollection(Of Orders)(From o In db.Orders Order By o.Customers.CustomerID Select o)

End Function

Public Sub New()

' Chiamata richiesta da Progettazione Windows Form.
InitializeComponent()

' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
ListBox1.ItemsSource = Lista()

End Sub
End Class


...così mi permette di sfruttare le ObservableCollection e converitre successivamente i SelectedItems in Orders...

Una domandina... è possibile raggrupparli, magari in base al CustomerID? Ho provato ad usare <ListBox.GroupStyle>, ma senza successo...


<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=CustomerID}" FontWeight="Bold"
Foreground="White" Background="LightGreen"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>


...l'ho inserito prima del <ListBox.ItemPanel>, tra quello e <ListBox.ItemTemplate>, in fondo a quest'ultimo, ma non raggruppa e toglie la "wrappatura" del ItemPanel...

Dove sbaglio?
You are not authorized to post a reply.

Active Forums 4.2
  
hd porn
 
© 2009-2011 WPF Tips&Tricks Team - Visual Basic Tips&Tricks Network
 
porno izleporno izle