English 中文(简体)
多绑定转换器?
原标题:Multibinding converter?
  • 时间:2010-10-23 04:31:00
  •  标签:
  • wpf
  • binding

我正在尝试为导航模型编写一个多绑定转换器,当从两个列表框中选择任何列表框项时,我会将页面加载到一个框架中。同时,我需要能够使用导航类中的“后退/前进”按钮进行导航,并且如果其UriSource加载到框架中,则能够以选定状态显示列表项。我拥有的覆盖器可以从框架中的两个源列表框更新uri源,但不能在选定状态下切换列表框项。我在转换器的ConvertBack部分将列表项切换到选定状态。我做错了什么,我总是在构建时出错。请让我知道是否有可能实现我正在努力做的事情。提前谢谢。

以下是MultiBindConverter代码:

 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>
问题回答

您没有实现IMultiValueConverter接口。ConvertBack的签名为:

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

它与Convert正好相反,Convert接收一个数组并输出一个值,因此它需要接收该单个值并输出一组值。在这种情况下,ConvertBack的传入值将是Frame.Source,输出将是2 SelectedValue属性。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签