如何从选定项目值的两个列表框更新帧的源? [英] How to update Frame source from two listboxes on selected item value?

查看:137
本文介绍了如何从选定项目值的两个列表框更新帧的源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望找到如何实现,我想有功能的答案。我有两个列表框。我从XML生成它们的内容。我在XML还定义UriSources。我想了解如何通过点击列表框任意列表框中的任何项目以实现导航功能。因此,我需要能够从两个不同的lisboxes更新帧的源属性。 Perphaps,它应该与一些变换器multibind。任何想法是非常AP preciated。

XAML:
列表框和框架:

 <窗​​口
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
的xmlns:D =htt​​p://schemas.microsoft.com/ex$p$pssion/blend/2008
的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
MC:可忽略=D
X:类=TwoListboxes_2.MainWindow
XMLNS:地方=CLR的命名空间:TwoListboxes_2
标题=主窗口
WIDTH =1000HEIGHT =700>
< Window.Resources>
    <局部:UriConverter X:键=UriConverter/>
    < XmlDataProvider X:键=PageData来源=数据/ data.xml中的XPath =/页/>
    <的DataTemplate X:键=SublevelListboxDataTemplate>
        <网格和GT;
            < TextBlock的文本={结合的XPath = @名}/>
        < /网格和GT;
    < / DataTemplate中>
    <的DataTemplate X:键=MainListBoxDataTemplate>
            < TextBlock的文本={结合的XPath = @名}/>
    < / DataTemplate中>
< /Window.Resources>
<电网的Horizo​​ntalAlignment =左VerticalAlignment =评出的>
        < Grid.ColumnDefinitions>
            < ColumnDefinition WIDTH =215/>
            &所述; ColumnDefinition宽度=1/>
            &所述; ColumnDefinition宽度=*/>
        < /Grid.ColumnDefinitions>
        <网格和GT;
            < ListBox的X:名称=SublevelListboxWIDTH =80
                     的DataContext ={结合的SelectedItem,的ElementName = Nav_ListBox}
                     的ItemTemplate ={DynamicResource SublevelListboxDataTemplate}
                     的ItemsSource ={结合的XPath = / *}
             />
            < BORDER =的Horizo​​ntalAlignment左>
                < ListBox的X:名称=Nav_ListBox
                         的ItemTemplate ={DynamicResource MainListBoxDataTemplate}
                         的ItemsSource ={绑定源= {StaticResource的PageData},XPath的=页}
                         SelectedValuePath =@ UriSource
                         的SelectedIndex =0
                />
            < /边框>
        < /网格和GT;
        <画框Grid.Column =2×:NAME =ContentFrameJournalOwnership =OwnsJournalNavigationUIVisibility =可见
               来源={绑定的SelectedValue,转换器= {StaticResource的UriConverter}的ElementName = Nav_ListBox,模式=双向}
            />
< /网格和GT;

  XML:< XML版本=1.0编码=UTF-8><页面的xmlns =>
    <页面名称=名称1UriSource =页/ Name1.xaml/>
    <页面名称=名称2UriSource =页/ Name2.xaml>
                    < 2级NAME =ALL1UriSource =页/ All1.xaml/>
            < 2级NAME =ALL2UriSource =页/ All2.xaml/>
                    < 2级NAME =ALL3UriSource =页/ All3.xaml/>
            < 2级NAME =ALL4UriSource =页/ All4.xaml/>
    < /页>
    <页面名称=3名UriSource =页/ Name3.xaml/>
    <页面名称=4名UriSource =页/ Name4.xamlIsEnabled =真/>

UriConverter到alllow listboxitems将在选中状态时,其UriSource装入框架:

 公共类MultiBindConverter:IMultiValueConverter
{
    #区域IMultiValueConverter成员    公共对象转换(对象[]值类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
    {        如果(值[0]!= NULL)
        {
            如果(值[1]!= NULL)
            {
                返回新的URI(值[1]的ToString(),UriKind.RelativeOrAbsolute);
            }
            返回新的URI(值[0]的ToString(),UriKind.RelativeOrAbsolute);
        }
        返回null;
    }    公用对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,System.Globalization.CultureInfo文化)
    {
        //抛出新NotImplementedException();        VAR URI =(URI)值;
        VAR uriString中= uri.OriginalString;
        如果(值!= NULL)
        {
            如果(uri.OriginalString.Contains(;组分/))
            {
                uriString中= uriString.Substring(uriString.IndexOf(/)+ 1);
            }
        }
        返回新对象[] {uriString中,uriString中[0],uriString中[1]};
    }
    #endregion IMultiValueConverter成员
}
先谢谢你。


解决方案

如果我理解正确,你都能够绑定两个列表框正确,唯一的问题是用两种不同的源绑定框架。
你的猜测是正确的,你可以做到这一点使用Multibinding。我假设第二列表已没有默认的选择。

在XAML中multibinding会是这样

 <框架Grid.Column =2×:NAME =ContentFrameJournalOwnership =OwnsJournalNavigationUIVisibility =可见>
            < Frame.Source>
                < MultiBinding转换器={StaticResource的CONV}>
                    <绑定路径=的SelectedItem的ElementName =Nav_ListBox/>
                    <绑定路径=的SelectedItem的ElementName =SublevelListbox/>
                < / MultiBinding>
            < /Frame.Source>
        < /帧>

您转换器将

 公共类MultiBindConverter:IMultiValueConverter {
        #区域IMultiValueConverter成员        公共对象转换(对象[]值类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化){
            如果(值[0]!= NULL){
                如果(值[1]!= NULL){
                    返回新的URI(值[1]的ToString(),UriKind.RelativeOrAbsolute);
                }
                返回新的URI(值[0]的ToString(),UriKind.RelativeOrAbsolute);
            }
            返回null;
        }        公用对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,System.Globalization.CultureInfo文化){
            抛出新NotImplementedException();
        }        #endregion IMultiValueConverter成员
    }

I hope to find an answer on how to achieve functionality I am trying to have. I have two listboxes. I generate content for them from XML. I also define UriSources in XML. I'd like to find out how to achieve a navigation by clicking any listbox item in any listbox. As a result, I need to be able to update Frame's Source property from two different lisboxes. Perphaps, it should be multibind with some converter. Any ideas are highly appreciated.

XAML: Listboxes and Frame:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"
x:Class="TwoListboxes_2.MainWindow"
xmlns:local="clr-namespace:TwoListboxes_2"
Title="MainWindow"
Width="1000" Height="700">
<Window.Resources>
    <local:UriConverter x:Key="UriConverter" /> 
    <XmlDataProvider x:Key="PageData" Source="Data/data.xml" XPath="/Pages" />
    <DataTemplate x:Key="SublevelListboxDataTemplate">
        <Grid>
            <TextBlock Text="{Binding XPath=@name}"/>   
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="MainListBoxDataTemplate">  
            <TextBlock Text="{Binding XPath=@name}"/>   
    </DataTemplate>
</Window.Resources>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top">   
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="215"/>
            <ColumnDefinition Width="1"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid >
            <ListBox x:Name="SublevelListbox" Width="80"
                     DataContext="{Binding SelectedItem, ElementName=Nav_ListBox}"
                     ItemTemplate="{DynamicResource SublevelListboxDataTemplate}" 
                     ItemsSource="{Binding XPath=./*}" 
             />
            <Border HorizontalAlignment="Left">
                <ListBox x:Name="Nav_ListBox" 
                         ItemTemplate="{DynamicResource MainListBoxDataTemplate}" 
                         ItemsSource="{Binding Source={StaticResource PageData}, XPath=page}"
                         SelectedValuePath="@UriSource"
                         SelectedIndex="0"
                />
            </Border>
        </Grid>
        <Frame Grid.Column="2" x:Name="ContentFrame" JournalOwnership="OwnsJournal" NavigationUIVisibility="Visible" 
               Source="{Binding SelectedValue, Converter={StaticResource UriConverter}, ElementName=Nav_ListBox, Mode=TwoWay}"
            />
</Grid> 

XML:

<?xml version="1.0" encoding="utf-8"?><Pages xmlns=""> 
    <page name="Name 1" UriSource="Pages/Name1.xaml"  /> 
    <page name="Name 2" UriSource="Pages/Name2.xaml"  > 
                    <level2 name="ALL1" UriSource="Pages/All1.xaml" /> 
            <level2 name="ALL2" UriSource="Pages/All2.xaml" /> 
                    <level2 name="ALL3" UriSource="Pages/All3.xaml" /> 
            <level2 name="ALL4" UriSource="Pages/All4.xaml" /> 
    </page> 
    <page name="Name 3" UriSource="Pages/Name3.xaml"/> 
    <page name="Name 4" UriSource="Pages/Name4.xaml" IsEnabled="True" /> 

UriConverter to alllow listboxitems to be in selected state when its UriSource is loaded into Frame:

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)
    {
        //throw new NotImplementedException();

        var uri = (Uri)value;
        var uriString = uri.OriginalString;
        if (value != null)
        {
            if (uri.OriginalString.Contains(";component/"))
            {
                uriString = uriString.Substring(uriString.IndexOf("/") + 1);
            }
        }
        return new object[] { uriString, uriString[0], uriString[1] };
    }
    #endregion IMultiValueConverter Members
} 
Thank you in advance.

解决方案

If I Understand correctly you are able to bind two list box correctly the only problem is with binding frame with two different source. your Guess is right you can achieve this using Multibinding. i am Assuming the second listbox has no default selection.

The multibinding in xaml will be like this

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

Your Converter will be

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) {
            throw new NotImplementedException ();
        }

        #endregion IMultiValueConverter Members
    }

这篇关于如何从选定项目值的两个列表框更新帧的源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆