数据绑定到两个源

发布于 2024-12-06 14:10:51 字数 3486 浏览 0 评论 0原文

我需要使用两个列表框,每个列表框绑定到不同的集合。

我最初使用一个列表框并绑定它,然后才需要绑定两个列表框。 我就是这样做的。

 <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>

我的代码只是将窗口的 DataContext 设置为 ObservableCollection ,

需要使用两个集合我创建了一个像这样的包装类

    public class AppModel
{


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

}

,我的 CS 现在将 DataContext 设置为 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)
            {
                _model.playerCollection = new ObservableCollection<Player>(_entity.Players);
            }
            return _model;
        }
        set { }

    }

在我的 Xaml 中,如何设置 datacontext现有列表框是否绑定到 AppModel 中的游戏集合? 一旦我开始工作,我将自己处理第二个列表框。 谢谢!

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)
            {
                _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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-12-13 14:10:51

您需要添加路径BindingDatacContext 将是模型,路径应指向任一集合:

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

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}" ...
佞臣 2024-12-13 14:10:51

将 Binding 更改为 能否解决您的问题?

根据您的问题,您声明您曾经将 DataContext 设置为 gameCollection,但现在您已将其更改为使用 AppModel,您还需要根据需要更改绑定。

这将从本质上改变 Binding,不再仅绑定到 gameCollection,现在将其设置为使用 AppData.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文