WPF功能区:ribboncombobx绑定问题 [英] WPF Ribbon: ribboncombobx binding problem

查看:74
本文介绍了WPF功能区:ribboncombobx绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I use a wpf ribbon (MS October 2010 version). On some of the ribbon-tabs are ribboncombobox-controls, which are bound to ObservableCollections.  The binding works fine, but just for these comboboxes, which are on the ribbon-tab, that is preselected in the xaml ribbon declaration with selectedindex=”x”. The other comboboxes has empty values (the gallery items are correct, though).

推荐答案

再次

我仍然有这个问题(请参阅第一篇文章).

I still have this problem (see first post).

这是一个显示问题的小例子.也许任何人都可以告诉我,我在这里做错了什么...

Here's a little example showing the problem. Maybe anyone can show me, what I'm doing wrong here...

感谢您的帮助,

汤姆

---

xaml:

<ribbon:RibbonWindow x:Class="WpfRibbonApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    Title="MainWindow"
		x:Name="RibbonWindow"
		Width="544" Height="318">

	<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ribbon:Ribbon x:Name="Ribbon">
      <ribbon:Ribbon.ApplicationMenu>
        <ribbon:RibbonApplicationMenu/>
      </ribbon:Ribbon.ApplicationMenu>
      
      <ribbon:RibbonTab x:Name="HomeTab" Header="Home">
        <ribbon:RibbonGroup x:Name="Group1" Header="Group1">
          
          <ribbon:RibbonComboBox Label="List1" IsEditable="False">
            <ribbon:RibbonGallery IsSynchronizedWithCurrentItem="True" 
                       SelectedValuePath="Value">
              <ribbon:RibbonGalleryCategory ItemsSource="{Binding Path=collection1}" 
                             DisplayMemberPath="Caption" />
            </ribbon:RibbonGallery>
          </ribbon:RibbonComboBox>

        </ribbon:RibbonGroup>
        
      </ribbon:RibbonTab>
      <ribbon:RibbonTab x:Name="OtherTab" Header="Other">
        <ribbon:RibbonGroup x:Name="Group2" Header="Group2">
          
          <ribbon:RibbonComboBox Label="List2" IsEditable="False">
            <ribbon:RibbonGallery IsSynchronizedWithCurrentItem="True" 
                       SelectedValuePath="Value" >
              <ribbon:RibbonGalleryCategory ItemsSource="{Binding Path=collection2}" 
                             DisplayMemberPath="Caption" />
            </ribbon:RibbonGallery>
          </ribbon:RibbonComboBox>

        </ribbon:RibbonGroup>

      </ribbon:RibbonTab>
    </ribbon:Ribbon>
    <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="92,59,0,0" Name="label1" VerticalAlignment="Top" Grid.Row="1" />
    <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="92,81,0,0" Name="label2" VerticalAlignment="Top" Grid.Row="1" />
    <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="301,59,0,0" Name="label3" VerticalAlignment="Top" Grid.Row="1" />
    <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="301,81,0,0" Name="label4" VerticalAlignment="Top" Grid.Row="1" />
  </Grid>
</ribbon:RibbonWindow>

---

和C#代码:


  public partial class MainWindow : RibbonWindow
  {
    public class Things
    {
      public string Caption { get; set; }
      public string Value { get; set; }

      public Things( string caption, string value )
      {
        Caption = caption;
        Value = value;
      }
    }
    
    public ObservableCollection<Things> collection1 { get; set; }
    public ICollectionView collectionview1;
    
    public ObservableCollection<Things> collection2 { get; set; }
    public ICollectionView collectionview2;



    public MainWindow()
    {
      InitializeComponent();

      collection1 = new ObservableCollection<Things>();
      collectionview1 = CollectionViewSource.GetDefaultView( collection1 );
      collectionview1.CurrentChanged += new EventHandler( collection1_current_changed );

      collection1.Add( new Things( "Test01", "Value01" ) );
      collection1.Add( new Things( "Test02", "Value02" ) );
      collection1.Add( new Things( "Test03", "Value03" ) );
      collectionview1.MoveCurrentToFirst();

      collection2 = new ObservableCollection<Things>();
      collectionview2 = CollectionViewSource.GetDefaultView( collection2 );
      collectionview2.CurrentChanged += new EventHandler( collection2_current_changed );

      collection2.Add( new Things( "Test04", "Value04" ) );
      collection2.Add( new Things( "Test05", "Value05" ) );
      collection2.Add( new Things( "Test06", "Value06" ) );
      collectionview2.MoveCurrentToFirst();

      this.DataContext = this;
    }

    private void collection1_current_changed( Object sender, EventArgs e )
    {
      if ( collectionview1.CurrentItem != null )
      {
        Things thisthing = collectionview1.CurrentItem as Things;

        label1.Content = thisthing.Caption;
        label2.Content = thisthing.Value;
      }
    }

    private void collection2_current_changed( Object sender, EventArgs e )
    {
      if ( collectionview2.CurrentItem != null )
      {
        Things thisthing = collectionview2.CurrentItem as Things;

        label3.Content = thisthing.Caption;
        label4.Content = thisthing.Value;
      }
    }

  }


这篇关于WPF功能区:ribboncombobx绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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