okipol
okipol

Reputation: 1219

WPF display member path to a method with parameters

I have a WPF ListBox, which i bind to some elements (which are not customizable by me). The binding itself is a sealed class too.

The element has a ToString which takes a CultureInfo parameter. I want to bind to that specific representation - by passing that parameter (I'll get it from the running thread). Is there anyway to specify the binding to show that specific representation?

XAML:

<ListBox Height="212" HorizontalAlignment="Left" Margin="6,6,0,0" 
    Name="listBoxTriggers" VerticalAlignment="Top" Width="183" />

Item Source:

listBoxTriggers.ItemsSource = _triggers

And code behind (the method i want to call)

trigger.ToString(cultureInfo);

Upvotes: 1

Views: 1431

Answers (1)

Bob Horn
Bob Horn

Reputation: 34305

Note: There's a good option to use a converter at this link.

If I understand correctly, you want your ListBox to show the ToString() using a specific culture. While that might be possible (see link mentioned above), a work-around would be to use a DTO that holds the ID of the trigger and the ToString() representation for which you're looking.

public class TriggerDto
{
    public int TriggerId { get; set; }
    public string TriggerName { get; set; }
}

Create a new List, then loop through all of your triggers, adding new TriggerDto objects.

List<TriggerDto> triggerDtos = new List<TriggerDto>();

foreach (Trigger trigger in _triggers)
{
    triggerDtos.Add(new TriggerDto() { Id = trigger.Id, TriggerName = trigger.ToString(cultureInfo) });
}

this.TriggerDtos = triggerDtos;

Set your binding to TriggerDtos.

When a user selects a TriggerDto, you'll just need to use its ID to grab the real Trigger object with which you want to work.

Disclaimer: Possible typos here. I typed the code directly into the answer and not in Visual Studio.

Upvotes: 1

Related Questions