Vì vậy, tôi có một Context Menu
Và một MenuItem
trong nó mà phá vỡ ra thành một danh sách các tên:Làm thế nào để thiết lập một biểu tượng trong MenuItem một ContextMenu khi sử dụng ItemContainerStyle
<ContextMenu>
<MenuItem Header="Set As Default For" ItemsSource="{Binding Source={StaticResource Names}}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value={Binding Name}/>
<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.DoSomething}" />
<Setter Property="CommandParameter" Value="{Binding }" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</ContextMenu>
Bây giờ các mã trên hoạt động tập tin và hiển thị của tôi danh sách tên. Bây giờ tôi muốn thêm một biểu tượng bên cạnh mỗi tên sử dụng một gói URI .. Vì vậy, từ this question tôi có thể thấy cách tốt nhất để làm điều đó là để template các Header
vì vậy tôi cố gắng mà lần đầu tiên như câu hỏi
<ContextMenu>
<MenuItem Header="Set As Default For" ItemsSource="{Binding Source={StaticResource Names}}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header">
<Setter.Value>
<StackPanel>
<Image Width="20" Height="20" Source="/MyProj;component/Resources/MyImg.png" />
<ContentPresenter Content="{Binding Name}" />
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.DoSomething}" />
<Setter Property="CommandParameter" Value="{Binding }" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</ContextMenu>
nhưng điều này đã cho tôi lỗi:
Specified element is already the logical child of another element. Disconnect it first.
Vì vậy, tôi sau khi một số nghiên cứu tôi đã cố gắng:
<ContextMenu>
<MenuItem Header="Set As Default For" ItemsSource="{Binding Source={StaticResource Names}}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Image Width="20" Height="20" Source="/MyProj;component/Resources/MyImg.png" />
<ContentPresenter Content="{Binding Name}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.DoSomething}" />
<Setter Property="CommandParameter" Value="{Binding }" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</ContextMenu>
nhưng bây giờ tất cả các tên của tôi là ControlTemplate và không có biểu tượng là màn hình ed ...
Làm cách nào để thêm biểu tượng vào Menu của Menu ngữ cảnh thông qua ItemContainerStyle?
EDIT
Tôi đã thử:
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Icon">
<Setter.Value>
<Image Width="20" Height="20" Source="/MyProj;component/Resources/MyImg.png" />
</Setter.Value>
</Setter>
Và tôi nhận được một biểu tượng render nhưng chỉ cho mục cuối cùng trong trình đơn của tôi?
Hoặc sử dụng 'HeaderTemplate' thay vì' Header' và sử dụng 'DataTemplate' thay vì' ControlTemplate' nhưng cá nhân tôi sẽ thiết lập 'sở hữu Icon' như một' Setter' trong 'bạn Style'. Mã của bạn sẽ đặt biểu tượng trong phần nội dung của 'MenuItem', không phải là phần' Biểu tượng' – dkozl
@dkozl bạn đang sử dụng 'HeaderTemplate' và' DataTemplate'. Và bạn cũng đã đúng khi mã của tôi đặt biểu tượng bên trong Header mà không phải là thứ tôi đã lên kế hoạch. Bạn có biết cách đặt biểu tượng bằng cách sử dụng Gói URI theo kiểu không? – user1
thử đặt ['x: Shared'] (https://msdn.microsoft.com/en-us/library/aa970778 (v = vs.110) .aspx) thành false đối với' Kiểu 'của bạn như' ' –
dkozl