Autor Beitrag
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 05.04.08 20:54 
Hallo!

ich versuche jetzt schon längere Zeit in WPF ein Kontextmenü bei Items in einer ListView anzuzeigen, nur bei Items, nicht in der ganzen ListView. In einem Forum hab ich gelesen, dass man den ItemContainerStyle der ListView entsprechend anpassen muss, jedoch kann man mit einem Setter keine ContextMenu's setzen. Ich hatte das folgendermaßen probiert:

ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <!-- ... -->
                    </ContextMenu>
                </Setter.Value>
             </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>


Hoffe ihr wisst wie das geht. :)

Chryzler
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 05.04.08 21:04 
Du benutzt doch bestimmt eine DataTemplate, kannst Du das da drin nicht machen? So z.B.

ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
        <ListView Margin="46,56,112,106" Name="listView1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Text}">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Foo" />
                                <MenuItem Header="Bar" />
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Chryzler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 05.04.08 21:13 
Hmm, eigentlich nicht. Die ListView verwendet ein GridView. Das ganze sieht im großen und ganzen so aus:
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>                        
    <ListView.View>                        
        <GridView>
            <GridViewColumn Header="Status">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>                                    
                        <Image Stretch="None" Source="{Binding Path=Status}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="File name" DisplayMemberBinding="{Binding FileName}" />
            <GridViewColumn Header="Progress">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=ProgressDescription, Mode=OneWay}" />
                            <ProgressBar Height="12" Foreground="#FFE400" Value="{Binding Path=Progress}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!-- ... -->
        </GridView>
    </ListView.View>                        
</ListView>


EDIT: Mein 1000. Beitrag :party:


Zuletzt bearbeitet von Chryzler am So 06.04.08 09:59, insgesamt 1-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 05.04.08 21:29 
Hm, okay. Ich hab nochmal gegoogelt: Anscheined muss man das Kontextmenü als Resource bauen, dann klappt das:

ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
    <Window.Resources>
        <ContextMenu x:Key="EinContextMenu">
            <MenuItem Header="Foo" />
            <MenuItem Header="Bar" />
        </ContextMenu>
        <Style x:Key="DerContainer" TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu" Value="{StaticResource EinContextMenu}" />
        </Style>
    </Window.Resources>

    <Grid>
        <ListView Margin="46,56,112,106" Name="listView1" ItemContainerStyle="{StaticResource DerContainer}" />
    </Grid>

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Chryzler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 05.04.08 21:42 
Cool, das geht schonmal. Fast jedenfalls. Wenn du mir jetzt noch sagst wie ich vom C#-Code aus auf die MenuItems zugreifen kann bin ich für heute zufrieden. :) Den Bezeichner "removeMenuItem" kennt er nämlich nicht.
ausblenden XML-Daten
1:
2:
3:
4:
5:
<Window.Resources>
    <ContextMenu x:Key="contextMenu">
        <MenuItem Name="removeMenuItem" Header="Remove" Click="removeMenuItem_Click" />
    </ContextMenu>
</Window.Resources>
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 05.04.08 21:46 
Von wo aus zugreifen? Aus dem Klick-Event natürlich einfach über den Sender, aber das meinst Du wahrscheinlich nicht ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Chryzler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 05.04.08 21:57 
Doch. :lol: Langsam werd ich alt, oder es liegt daran dass es schon etwas später ist. Ich denk ich mach morgen weiter.
Danke fürs Helfen nochmal!