views:

84

answers:

1

I am tryng to write a multibinding converter for a navigation model where I load pages into a frame when any listboxitem is selected from two listboxes. At the same time, I need to be able to navigate with Back/Forward buttons from the navigation class and being able to show listboxitem in selected state if its UriSource is loaded into the frame. The coverter I have can update urisources from two sources-listboxes in the frame but cannot switch listboxitem in selected state. I switch listboxitem into selected state on ConvertBack portion of the converter. I did something wrong, I am always getting errors on a built. Please let me know if it possible to achieve what I m trying to do. Thank you in advance.

The below is the MultiBindConverter code:

 public class MultiBindConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] != null)
        {
            if (values[1] != null)
            {
                return new Uri(values[1].ToString(), UriKind.RelativeOrAbsolute);
            }
            return new Uri(values[0].ToString(), UriKind.RelativeOrAbsolute);
        }
        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    { 
        if (value != null)
        {
            var uri = (Uri)value;
            var uriString = uri.OriginalString;

            if (uri.OriginalString.Contains(";component/"))
            {
                uriString = uriString.Substring(uriString.IndexOf("/") + 1);
            }
            return new object[] { uriString, uriString };  
       }
   } 
} 

XAML:

<Frame Grid.Column="2" x:Name="ContentFrame" JournalOwnership="OwnsJournal" NavigationUIVisibility="Visible"> 
        <Frame.Source> 
            <MultiBinding Converter="{StaticResource MultiBindConverter}"> 
                <Binding Path="SelectedValue" ElementName="Nav_ListBox"/> 
                <Binding Path="SelectedValue" ElementName="SublevelListbox"/> 
            </MultiBinding> 
        </Frame.Source> 
    </Frame>
+1  A: 

You aren't implementing the IMultiValueConverter interface. The signature for ConvertBack is:

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)

It is the exact opposite of Convert which takes in an array and outputs a single value so it needs to take in that single value and output an array of values. The incoming value for ConvertBack in this case would be the Frame.Source and the outputs would be the 2 SelectedValue properties.

John Bowen
Thank you for looking at my post. It is that I have hard time doing correctly. I am wondering if you will have a chance to show how it will be in the code lines. I tried to play with it long enough and still stuck here.
vladc77
I modified the code but still have problem to run it. Please look at the code above. I changed the converter.
vladc77
To get values back to the SelectedValue properties you'll need to return a 2 element array from ConvertBack with the values in the order of the Bindings. If you wanted the same value for both it could be like: return new object[] { uriString, uriString }; You also need to set Mode="TwoWay" on the Bindings.
John Bowen
I tried that. I do something wrong. I am wondeing if you wouldn't mind to show me how it will be all in the code. I changed my converter and updated it above. It is still not working. I need to get back values which will be related to each listboxitem of any listbox. It is a little bit tricky for me. Thank you for looking my thread. I highly appreciate it.
vladc77
I am getting error: "Error 1 'TwoListboxes_2.MultiBindConverter.ConvertBack(object, System.Type[], object, System.Globalization.CultureInfo)': not all code paths return a value C:\Users\VC\Desktop\WPF Dynamic Menus\TwoListboxes_2\TwoListboxes_2\MultiBindConverter.cs" Thank you.
vladc77
I still cannot solve this problem. Any Ideas??
vladc77
Your current code isn't returning anything if value==null. Just like any other C# method with a return type all paths need to return something.
John Bowen
I did many revisions of this converter. It is something that I got stuck. I cannot make it work. It looks like the ConvertBack method is never called. I am just wondering if you know that is posiible to achieve what I am planning to do in one converter.
vladc77
John, I uploaded the solution here: http://cid-0c29483cf3a6a14d.office.live.com/self.aspx/WPF%5E_Tests/TwoListboxes%5E_2.zip
vladc77
I modified the converter there again. It will be great if you have a time to take a look at it. Nagigationg throught the pages works, but Listboxitems selected state is not getting to be selected while navigate with Back/Forward buttons of the frame. I highly value your input. Thank you in advance.
vladc77