开发者

DataBinding To Two Sources

I have a need to use two listboxes, each bound to a different collection.

i originally had this working with one listbox and binding before the need to bind two came up. Here is how I was doing that.

 <Window x:Class="TeamManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc ="clr-namespace:TeamManager"
    Title="Game Manager" Height="800" Width="800">

<Window.Resources>
    <DataTemplate DataType="{x:Type loc:Game}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="100"></ColumnDefinition>
                <ColumnDefinition Width="100"></ColumnDefinition>
                <ColumnDefinition Width="100"></ColumnDefinition>
                <ColumnDefinition Width="100"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Name="dateBlock" Grid.Column="0" Grid.Row="1" Text="{Binding Date,  StringFormat=d}"></TextBlock>
            <TextBlock Name="TimeBlock" Grid.Column="1" Grid.Row="1" Text="{Binding Time}"></TextBlock>
            <Button Grid.Row="1" Grid.Column="2" CommandParameter="{Binding Id}" Click="Manage_Click" >Manage</Button>
            <Button Grid.Row="1" Grid.Column="3" CommandParameter="{Binding Id}" Click="Delete_Click" Height="16" Width="16">
                <Image Source="/Images/DeleteRed.png"></Image>

            </Button>
        </Grid>
    </DataTemplate>
</Window.Resources>
<StackPanel>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">

        <StackPanel>
            <TextBlock>Upcomming Games</TextBlock>
            <ListBox ItemsSource="{Binding}" Name="GameList"></ListBox>
        </StackPanel>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
            <Button Height="30" Width="100" Margin="10,10,10,10" Click="AddGame_Click">Add New Game</Button>
        </StackPanel>
    </StackPanel>
</StackPanel>

And my code simply set the DataContext of the window to a ObservableCollection

with the need to use TWO collections I created a wrapper class like this

    public class AppModel
{


    public ObservableCollection<Game> gameCollection { get; set; }
    public ObservableCollection<Player> playerCollection { get; set; }

}

And my CS is now setting the DataContext to an object of AppModel

    GameDBEntities _entity = new GameDBEntities();
    AppModel _model;
    public MainWindow()
    {
          InitializeComponent();

          DataContext = model;
    }


    AppModel model
    {
        get
        {
            if (_model == null)
            {
                _model = new AppModel();
            }
            if (_model.gameCollection == null)
            {
                _model.gameCollection = new ObservableCollection<Game>(_entity.Games);
            }
            if (_model.playerCollection == null)
            {
              开发者_JS百科  _model.playerCollection = new ObservableCollection<Player>(_entity.Players);
            }
            return _model;
        }
        set { }

    }

In my Xaml, how can I set the datacontext of the existing listBox to be bound to the Collection Of Games in The AppModel? Once I get that working I will work on the second listbox on my own. Thanks!


You need to add a Path to the Binding. The DatacContext will be the model, the path should point to either collection:

<ListBox ItemsSource="{Binding gameCollection}" ...


Would changing the Binding to <ListBox ItemsSource="{Binding Path=gameCollection}" Name="GameList"></ListBox> solve your problem?

As per your question you state that you used to set the DataContext to the gameCollection, but now that you have changed this to use the AppModel, you will need to also change your binding as appropriate.

This will essentially change the Binding from being just bound to gameCollection, it will now be set to use AppData.gameCollection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜