使用 IEnumerable 集合在 Item 模板内绑定 ComboBox

发布于 2025-01-11 03:03:51 字数 2783 浏览 0 评论 0原文

我有一个类MovieSystem,它继承自AvailableMovies集合。

public class MovieSystem  : IEnumerable<AvailableMovies >, IEnumerable
{
    public AvailableMovies this[int i] { get; }
    public AvailableMovies this[TheatreLocation location] { get; }
    public AvailableMovies [] ReleasedMovies { get; }
 }

TheatreLocation 是一个枚举,其值类似于

public enum TheatreLocation 
{
        Citycentre = 0,
        CityNorth = 1,
        CitySouth = 2,
        CityEast = 3,
        CityWest = 4,
}

可用电影集合,具有以下属性

public class AvailableMovies 
{
        public AvailableMovies ();
        public int Index { get; }
        public TheatreLocation Location { get; set; }
        public string TheatreID { get; set; }
        public double Ticketprice{ get; }
}

我的 Xaml 代码如下所示

<ItemsControl ItemsSource="{Binding MovieSystem  }">            
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                            <ComboBox 
                                      ItemsSource="{Binding Path= ""What should I bind here? ""}"
                                      DisplayMemberPath="{Binding Path = ???}"
                                      SelectedIndex="{Binding Path =Location , Mode=TwoWay, Converter={StaticResource MovieSystemLocationConverter}}">
                            </ComboBox>
                            <ComboBox
                                      ItemsSource="{Binding Path = ???}"
                                      SelectedIndex="{Binding Path=TheatreID , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                      DisplayMemberPath="{Binding Path= ??}">
                            </ComboBox>
                            <TextBox Grid.Column="3"
                                     Text="{Binding Path= Ticketprice, Mode =TwoWay,StringFormat=F3, NotifyOnValidationError=True}"/>
                                    
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
</ItemsControl>

我有一个视图模型,xaml 数据上下文设置为此视图模型。

internal class MovieSystemPanelViewModel
{
        ///tried this collection but not worked.
        public ObservableCollection<LocationViewModel> Locations { get; } = new ObservableCollection<LocationViewModel>();
   
        public MovieSystem MovieSystem { get; }
              
        public MovieSystemPanelViewModel() {}
}

现在我的问题是 ComboBox 应该显示剧院 ID,而位置由于绑定问题而未显示。如果尝试在 ComboBox itemsource 内绑定 Locations 可观察集合,则不允许,在 items 模板内,只有 AvailableMovies 属性处于可绑定状态。 Ticketprice 已正确绑定并显示。

I have one class MovieSystem which inherits from AvailableMovies collection.

public class MovieSystem  : IEnumerable<AvailableMovies >, IEnumerable
{
    public AvailableMovies this[int i] { get; }
    public AvailableMovies this[TheatreLocation location] { get; }
    public AvailableMovies [] ReleasedMovies { get; }
 }

TheatreLocation is an enum with values like

public enum TheatreLocation 
{
        Citycentre = 0,
        CityNorth = 1,
        CitySouth = 2,
        CityEast = 3,
        CityWest = 4,
}

AvailableMovies collection is with below properties

public class AvailableMovies 
{
        public AvailableMovies ();
        public int Index { get; }
        public TheatreLocation Location { get; set; }
        public string TheatreID { get; set; }
        public double Ticketprice{ get; }
}

My Xaml code looks like below

<ItemsControl ItemsSource="{Binding MovieSystem  }">            
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                            <ComboBox 
                                      ItemsSource="{Binding Path= ""What should I bind here? ""}"
                                      DisplayMemberPath="{Binding Path = ???}"
                                      SelectedIndex="{Binding Path =Location , Mode=TwoWay, Converter={StaticResource MovieSystemLocationConverter}}">
                            </ComboBox>
                            <ComboBox
                                      ItemsSource="{Binding Path = ???}"
                                      SelectedIndex="{Binding Path=TheatreID , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                      DisplayMemberPath="{Binding Path= ??}">
                            </ComboBox>
                            <TextBox Grid.Column="3"
                                     Text="{Binding Path= Ticketprice, Mode =TwoWay,StringFormat=F3, NotifyOnValidationError=True}"/>
                                    
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
</ItemsControl>

I have a view model, xaml datacontext is set to this view model.

internal class MovieSystemPanelViewModel
{
        ///tried this collection but not worked.
        public ObservableCollection<LocationViewModel> Locations { get; } = new ObservableCollection<LocationViewModel>();
   
        public MovieSystem MovieSystem { get; }
              
        public MovieSystemPanelViewModel() {}
}

Now my problem is both ComboBox which should display Theatre id and location not displaying due to binding issue. If try to bind that Locations observable collection inside ComboBox itemsource its not allowing, inside items template only AvailableMovies properties are in bindable condition.
Ticketprice is binding properly and displaying.

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

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

发布评论

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

评论(1

明媚如初 2025-01-18 03:03:51

这里的问题是,由于您将 Items.Control 与集合绑定,因此该 Items.Control 的子元素将只能访问该集合的属性。因此,如果您想使用另一个集合,则需要使用相对源来获取datacontext级别的访问权限。由于它是一个要正确显示的 enum ,因此您需要使用扩展方法显示字典值,并在 ComboBox 内使用另一个 Itemtemplate 来获取正确的显示。

internal class MovieSystemPanelViewModel
{
        ///Use  dictionary instead of observable collection
        public Dictionary<TheatreLocation , string> Locations { get; } = new Dictionary<TheatreLocation , string>();
   
        public MovieSystem MovieSystem { get; };
              
        public MovieSystemPanelViewModel() 
        {
            // Fill up data in dictionary
            foreach (Enum item in Enum.GetValues(typeof(TheatreLocation)))
            {
                Locations.Add((TheatreLocation)item, ((TheatreLocation)item).GetDisplayName());
            }
        }
}

在项目某处写一个Extension方法,用于根据需求显示成员。

public static string GetDisplayName(this TheatreLocation location)
{
            switch (location)
            {
                case TheatreLocation.CityCentre:
                    return "Center city location";

                case TheatreLocation.CityNorth :
                    return "Northern city location";

                case TheatreLocation.CitySouth :
                    return "Southern city location";

                case TheatreLocation.CityEast :
                    return "Eastern city location";

                case CameraLocation.CityWest :
                    return "Western city location";

                 default:
                    return "Unknown";
            }
}

现在将 xaml 与这些属性和方法绑定。

<ComboBox 
         ItemsSource="{Binding Path= DataContext.Locations, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
          SelectedIndex="{Binding Path =Location , Mode=TwoWay, Converter={StaticResource MovieSystemLocationConverter}}">
            <ComboBox.ItemTemplate>
                  <DataTemplate>
                             <TextBlock Text="{Binding Path=Value}" />
                   </DataTemplate>
              </ComboBox.ItemTemplate>
</ComboBox>

The issue here is since you are binding Items.Control with a collection, child elements of that Items.Control will have access to that collection's property only. So, if you want to use another collection you need to use relative source to get access in datacontext level. And since it's an enum to properly display, you need to display the dictionary values using an extension method and also using another Itemtemplate inside ComboBox to get the proper display.

internal class MovieSystemPanelViewModel
{
        ///Use  dictionary instead of observable collection
        public Dictionary<TheatreLocation , string> Locations { get; } = new Dictionary<TheatreLocation , string>();
   
        public MovieSystem MovieSystem { get; };
              
        public MovieSystemPanelViewModel() 
        {
            // Fill up data in dictionary
            foreach (Enum item in Enum.GetValues(typeof(TheatreLocation)))
            {
                Locations.Add((TheatreLocation)item, ((TheatreLocation)item).GetDisplayName());
            }
        }
}

Write a Extension method in project somewhere, for display member according to requirement.

public static string GetDisplayName(this TheatreLocation location)
{
            switch (location)
            {
                case TheatreLocation.CityCentre:
                    return "Center city location";

                case TheatreLocation.CityNorth :
                    return "Northern city location";

                case TheatreLocation.CitySouth :
                    return "Southern city location";

                case TheatreLocation.CityEast :
                    return "Eastern city location";

                case CameraLocation.CityWest :
                    return "Western city location";

                 default:
                    return "Unknown";
            }
}

Now Bind xaml with these properties and methods.

<ComboBox 
         ItemsSource="{Binding Path= DataContext.Locations, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
          SelectedIndex="{Binding Path =Location , Mode=TwoWay, Converter={StaticResource MovieSystemLocationConverter}}">
            <ComboBox.ItemTemplate>
                  <DataTemplate>
                             <TextBlock Text="{Binding Path=Value}" />
                   </DataTemplate>
              </ComboBox.ItemTemplate>
</ComboBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文