Berryl
Berryl

Reputation: 12833

Databinding to show an assembly name

I have a ComboBox of assemblies, and I would like to only display the assembly Name, as opposed to the FullName. The equivalent code of what I want to do in the binding is

 asm.GetName().Name

I'm tagging this as a c# question also since maybe there is a property on Assembly I don't know about.

Here is the relevant part of the XAML I am using:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding FullName}" /> ** just the Name please
    </DataTemplate>
</ComboBox.ItemTemplate>

What's a good way to do this?

Cheers,
Berry

Upvotes: 2

Views: 294

Answers (2)

sll
sll

Reputation: 62524

As a case you can bind combobox to a dictionary, where Key would be an assembly name and Value - assembly itself so you would be able bind to assembly name and other properties as well.

allAssembliesMap.Add(assembly.GetName().Name, assembly);

public IDictionary<string, Assembly> AllAssemblies 
{ 
    get 
    { 
       return allAssembliesMap; 
    } 
}
<ComboBox ItemsSource="{Binding AllAssemblies}"
          DisplayMemberPath="Key"
          SelectedValuePath="Value" />

Upvotes: 2

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35136

Write a value converter which will call GetName method and return Name.

public class AssemblyNameConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var assembly = (Assembly)value;
        return assembly.GetName().Name;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
         throw new NotImplementedException();
    }
}

Upvotes: 3

Related Questions