0

Can use IValueConverter with List. It can use in the first time I call the menu. When I updated the items in list, it don't call IValueConverter again? Example:

                    <MenuItem Header="{Binding Path=DataContext.Documents, RelativeSource={RelativeSource FindAncestor,
    AncestorType={x:Type ListView}}, Converter={StaticResource DocumentsToString}, Mode=OneWay}">
                    <MenuItem.Icon>
                        <Image Source="Images/upload.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>

And ValueConverters.cs

public class ListDocumentToStringConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var selectedDocuments = (ObservableCollection<Document>) value;
        var result = "";
        foreach (var document in selectedDocuments)
        {
            result += document.Name + "\t";
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
  • The converter is only called if the `Documents` property gets a new value; the converter is not called if the collection referenced by the `Documents` property remains the same, even if `Document` items are added to that collection. Do you really want to write all document names into a single `MenuItem`? Or do you want to create one `MenuItem` per document? If the latter is the case, assign your collection to `Menu.ItemsSource`. –  Jun 02 '12 at 05:40
  • I use Document.SelectedCount and If I click another item in Document, it call converter again – Án Bình Trọng Jun 13 '12 at 04:07

1 Answers1

0

To add to fmunkert's comment which is correct, if you are intent to host all of those items within a single MenuItem, you could write something like this:

<MenuItem>
    <MenuItem.Header>
        <ItemsControl ItemsSource="{Binding Path=DataContext.Documents, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </MenuItem.Header>
    <MenuItem.Icon>
        <Image Source="Images/upload.png" Style="{StaticResource ImageContextMenu}"/>
    </MenuItem.Icon>
</MenuItem>

This way you don't have to use a converter at all. If you want to change how these items are laid out with respect to each other, such as using tabs in between in your example, then you would want to template the ItemsPanel of the ItemsControl. By default it is a vertical StackPanel. You could change it to a Horizontal StackPanel like so:

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

Then you can add spacing or a margin to each TextBlock item to achieve however visual you want.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102