将两个ComboBox绑定到同一个源WPF

时间:2013-08-27 21:07:13

标签: c# wpf xaml combobox itemssource

我有以下XAML,其中我有两个具有相同ItemsScource的组合框控件。我使用IsSynchronizedWithCurrentItem=False尝试让他们单独行动。但仍在改变另一个会改变另一个。

XAML

<Page x:Class="HaMmeR.FantasyFootball.TradeEvalPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="TradeEvalPage">
    <Grid x:Name="masterPanel">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0" Grid.Column="0" Style="{DynamicResource dockLsLabel}">
            <Label  x:Name="teamName1Lbl" Content="Team Name" Style="{DynamicResource lsLabel}" />
            <!--IsSynchronizedWithCurrentItem=False Treat datasource as individual-->
            <ComboBox x:Name="teamName1Cmb"  Margin="2" DockPanel.Dock="Right"
                              ItemsSource="{Binding Path=FantasyTeams}"
                      IsSynchronizedWithCurrentItem="False"
                              DisplayMemberPath="TeamName"
                              SelectionChanged="teamName1Cmb_SelectionChanged"
                              SelectedValuePath="TeamName" SelectedValue="{Binding Path=TeamName}"/>
        </DockPanel>
        <DockPanel Grid.Row="0" Grid.Column="1" Style="{DynamicResource dockLsLabel}">
            <Label  x:Name="teamName2Lbl" Content="Team Name" Style="{DynamicResource lsLabel}" />
            <ComboBox x:Name="teamName2Cmb"  Margin="2" DockPanel.Dock="Right"
                              ItemsSource="{Binding Path=FantasyTeams}"
                       IsSynchronizedWithCurrentItem="False"
                              DisplayMemberPath="TeamName"
                              SelectionChanged="teamName2Cmb_SelectionChanged"
                              SelectedValuePath="TeamName" SelectedValue="{Binding Path=TeamName}"/>
        </DockPanel>
    </Grid>
</Page>

.cs文件

 public partial class TradeEvalPage : Page
    {
        private FantasyLeague league;
        public TradeEvalPage(FantasyLeague League)
        {
            InitializeComponent();
            league = League;

            //FantasyTeamCollection team1Source = new FantasyTeamCollection(league.FantasyTeams.ToList<FantasyTeam>());
            //FantasyTeamCollection team2Source = new FantasyTeamCollection(league.FantasyTeams.ToList<FantasyTeam>());
            //teamName1Cmb.DataContext = team1Source;
            //teamName2Cmb.DataContext = team2Source;
            masterPanel.DataContext = league;


            //mainContent.Text = string.Format("You clicked Trade Evalution of {0}", league.LeagueName);
        }
     }

我无法弄清楚我做错了什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为因为您将selectedValue绑定到TeamName。

SelectedValue =“{Binding Path = TeamName}”

如果删除绑定,它们可能会起作用。我想这里发生的是,当您在一个组合框中更改选择时,新选择项的值将写入当前项名称。所以它看起来第一个组合框也是这样,但是如果你打开它,你会看到这个值两次。