Frage

I need a WPF control that functions similar to the 'Resolve Conflicts' window in TFS, and other similar source control systems.

I have the following classes

 public class Conflict:INotifyPropertyChanged
{
    private string _name;
    private List<Resolution> _resolutions;
    private bool _focused;
    private bool _hasResolutions;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public List<Resolution> Resolutions
    {
        get { return _resolutions; }
        set
        {
            _resolutions = value;
            OnPropertyChanged("Resolutions");
        }
    }

    public bool Focused
    {
        get { return _focused; }
        set { 
            _focused = value;
            OnPropertyChanged("Focused");
        }
    }

    public bool HasResolutions

    {
        get { return _resolutions.Any(); }
        set
        {
            _hasResolutions = value;
            OnPropertyChanged("HasResolutions");
        }
    }
}

public class Resolution
{
    public string Name { get; set; }

    public void Resolve()
    {
        //Logic goes here
    }
}

This almost identical to the functionality of the Team Foundation Server (TFS) 'Resolve Conflict' window shown below:

enter image description here

For each row in the image above, it is the same as my Conflcit object, and for each of the buttons, would be one of the Resolution objects on the Conflict object.

My plan was to bind my List to a ListView, and then write a custom template or whatever to hide/show the buttons below it based on if it was selected or not.

To try to simplify what I need to accomplish, I have a List and I want to bind it to a control, and it look as close to the image above as possible.

How would I accomplish this and XAML and the code behind?

War es hilfreich?

Lösung

Here is an example of how you can dynamically create a data template, and add buttons based on your Conflict objects:

    public DataTemplate BuildDataTemplate(Conflict conflict)
    {
        DataTemplate template = new DataTemplate();

        // Set a stackpanel to hold all the resolution buttons
        FrameworkElementFactory factory = new FrameworkElementFactory(typeof(StackPanel));
        template.VisualTree = factory;

        // Iterate through the resolution
        foreach (var resolution in conflict.Resolutions)
        {
            // Create a button
            FrameworkElementFactory childFactory = new FrameworkElementFactory(typeof(Button));

            // Bind it's content to the Name property of the resolution
            childFactory.SetBinding(Button.ContentProperty, new Binding("Name"));
            // Bind it's resolve method with the button's click event
            childFactory.AddHandler(Button.ClickEvent, new Action(() => resolution.Resolve());

            // Append button to stackpanel
            factory.AppendChild(childFactory);
        }

        return template;
    }

You can do this in many different ways and this is just one of them. I haven't test it, but this should be enough to get you started :)

Good luck

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top