Question

I have the following XAML where I have two combobox controls with the same ItemsScource. I used the IsSynchronizedWithCurrentItem=False to attempt to get them to act individually. But still changing one changes the other.

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 File

 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);
        }
     }

I can't figure out what I'm doing wrong. Any help would be appreciated.

Was it helpful?

Solution

I think because you Bind the selectedValue to the TeamName.

SelectedValue="{Binding Path=TeamName}"

If you remove the bind, they probably work. I think whats happen here is, when you change the selection in one combobox, the value of the new selected item is written in the current items-name. So it looks that the first combobox does the same, but if you open it, you'll see that value twice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top