
Ergebnis 1 bis 6 von 6
-
- 12.05.2011, 10:56
- #1
Hey Leute
Steh mal wieder auf dem Schlauch. Es ist für mich nicht ganz einfach mein Problem zu beschreiben, darum habe ich auch Mühe gehabt etwas Hilfreiches zu finden(und darum auch der beschi$$ene Titel).
Es ist so:
Ich habe eine ListBox und darin ein StackPanel.
[FONT=Consolas]<[/FONT][FONT=Consolas]Grid[/FONT][FONT=Consolas] x[/FONT][FONT=Consolas]:[/FONT][FONT=Consolas]Name[/FONT][FONT=Consolas]="ContentPanel"[/FONT][FONT=Consolas] Grid.Row[/FONT][FONT=Consolas]="1"[/FONT][FONT=Consolas] Margin[/FONT][FONT=Consolas]="12,0,0,90">[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]ListBox[/FONT][FONT=Consolas] Height[/FONT][FONT=Consolas]="508"[/FONT][FONT=Consolas] HorizontalAlignment[/FONT][FONT=Consolas]="Left"[/FONT][FONT=Consolas] Name[/FONT][FONT=Consolas]="listBox1"[/FONT][FONT=Consolas] VerticalAlignment[/FONT][FONT=Consolas]="Top"[/FONT][FONT=Consolas] Width[/FONT][FONT=Consolas]="468"[/FONT][FONT=Consolas] SelectionChanged[/FONT][FONT=Consolas]="listBox1_SelectionChanged">[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]ListBox.ItemTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]DataTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]StackPanel[/FONT][FONT=Consolas] Orientation[/FONT][FONT=Consolas]="Horizontal"[/FONT][FONT=Consolas] Width[/FONT][FONT=Consolas]="460"[/FONT][FONT=Consolas] MouseLeftButtonUp[/FONT][FONT=Consolas]="StackPanel_MouseLeftButtonUp">[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]="{[/FONT][FONT=Consolas]Binding[/FONT][FONT=Consolas] name[/FONT][FONT=Consolas]}" />[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]=" "/>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]="{[/FONT][FONT=Consolas]Binding[/FONT][FONT=Consolas] pro[/FONT][FONT=Consolas]}" />[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]=" "/>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]="{[/FONT][FONT=Consolas]Binding[/FONT][FONT=Consolas] ang[/FONT][FONT=Consolas]}" />[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]StackPanel[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]DataTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]ListBox.ItemTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]ListBox[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBox[/FONT][FONT=Consolas] Height[/FONT][FONT=Consolas]="72"[/FONT][FONT=Consolas] HorizontalAlignment[/FONT][FONT=Consolas]="Left"[/FONT][FONT=Consolas] Margin[/FONT][FONT=Consolas]="8,288,0,0"[/FONT][FONT=Consolas]</[/FONT][FONT=Consolas]Grid[/FONT][FONT=Consolas]>[/FONT]
Ich will die StackPanel "Zeile" anklicken können und dann soll etwas passieren(wie ein Button).
Dazu habe ich einen MouseLeftButtonUp EventHandler definiert.
[FONT=Consolas]private[/FONT][FONT=Consolas] void StackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)[/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] TuDies;[/FONT]
[FONT=Consolas] }[/FONT]
Soweit so gut. Nun meine Frage:
Kann ich diesem EventHandler eine Art ID aus dem StackPanel mitgeben? So das je nach dem welche Zeile ich anklicke etwas passiert dass sich auf den Inhalt des StackPanels bezieht, also auf die 3 Variablen name, pro, ang?
Für einen kleinen Tipp wäre ich sehr dankbar.
Gruss
-
Fühle mich heimisch
- 13.05.2011, 10:44
- #2
Da gibt es verschiedene Möglichkeiten...
am einfachsten, nimmst du ein Button her statt ein TextBlock, da kannst du den Button Template so anpassen, dass es gleich aussieht. Da hast du dann ein Click event für jeden button:
Beim 'Resources':
Code:<ControlTemplate Key="ButtonTemplate" TargetType="Button"> <Grid Margin="{TemplateBinding Margin}" Background="{TemplateBinding Background}"> <ContentPresenter Margin={TemplateBinding Padding}"/> </Grid> </ControlTemplate>
Code:<Button Template="{StaticResource ButtonTemplate}" Click="..." Content="{Binding ...}"/>
Code:Button button = (Button)sender; MyDataType data = (MyDataType)button.DataContext; ...
Calum
-
- 16.05.2011, 13:35
- #3
Hey calum
Danke für Deine Antwort.
Hab mal etwas experimentiert, jedoch schnall ich das noch nicht ganz.
Wo genau kommt der 'Resources' Code hin?
und auf was greift das .DataContext zu?
Gruss
Reto
-
Fühle mich heimisch
- 19.05.2011, 10:48
- #4
Hi Reto,
Sorry dass es ein bisschen gedauert hat.
Die 'Resources' block gehört in dem Page.xaml oder in App.xaml rein (am besten App.xaml wenn du das Template über mehrere Seiten verwenden willst). Normalerweise ist ein leeres Block in App.xaml schon drin.
Code:<PhoneApplicationPage ... > <PhoneApplicationPage.Resources> <ControlTemplate Key="ButtonTemplate" TargetType="Button"> <Grid Margin="{TemplateBinding Margin}" Background="{TemplateBinding Background}"> <ContentPresenter Margin={TemplateBinding Padding}"/> </Grid> </ControlTemplate> </PhoneApplicationPage.Resources> <Grid x:Name="LayoutRoot"> .... </Grid> </PhoneApplicationPage>
Wenn du ItemsSource für eine Liste setzt, dann ist der DataContext für jeder ListItem das passende Objekt aus deine Collection (und, sofern du nicht explicit DataContext für einen Element setzt, auch alle Elemente in der Hierarchy nach unten).
Ich hoffe, dass man das so verstehen kann... wenn nicht dann schreib ich's halt auf Englisch
Gruß
Calum
-
- 24.05.2011, 07:48
- #5
Hi Calum
Danke, bin wieder etwas weiter gekommen.
Nun noch eine Frage:
Im Content Binding kann ich momentan nur eine Variable verwenden, also so:
Content="{Binding name}"
Gibt es eine Möglichkeit dort mehrere Variablen zu verwenden?
Ungefähr so:
Content="{Binding name, pro, ang}"
Gruss
Reto
-
Fühle mich heimisch
- 24.05.2011, 08:27
- #6
Hi Reto,
leider nicht. In dem Fall nehme ich entweder 3 TextBlocks mit entsprechende Werte für Margin, alle in ein horizontal ausgerichtete StackPanel. Oder ich füge noch ein Property zum Object hinzu, der aus die drei bestehende das richtige Text zurückgibt (das mache ich z.B. um ein formatiertes Zahl darzustellen, da es erst ab Silverlight 4 ein StringFormat Parameter für ein Binding gibt).
Eine weitere Alternativ wäre ein Converter zu nehmen, finde ich aber in diesem Fall übertrieben. Ein Converter ist eine Klasse in dem zwei Methode implementiert sind (vom Interface System.Windows.Data.IValueConverter), um den gebundene Wert umzuwandeln. Ich nehme das öfters her um ein Boolean in ein Visibility Wert umzuwandeln, oder ein URI in ein Image).
Gruß
Calum
Ähnliche Themen
-
Ein + zwischen zwei Seiten übergeben
Von Cr3dos im Forum Windows Phone 7 EntwicklungAntworten: 1Letzter Beitrag: 17.04.2011, 11:19
Pixel 10 Serie mit Problemen:...