c# - 将字典值 DataBind 到 ObservableCollection C# - XAML

标签 c# wpf xaml dictionary binding

我有一个自定义对象的 Observable 集合和一个公共(public)字典变量。

我希望“BrandName”属性充当“Brands”字典的键并将颜色绑定(bind)到按钮。我该怎么做呢?字典变量位于类之外。

C# 代码:

private ObservableCollection<BusService> BusServicesGUI;
public Dictionary<String, Brush> Brands;

public MainWindow(Dictionary<String, BusService> busServices)
{
    InitializeComponent();
    BusServicesGUI = new ObservableCollection<BusService>(BusServices.Values);
    lstMachineFunctions.ItemsSource = BusServicesGUI;
    lstMachineFunctions.Items.Refresh();
}

C# 类:

public class BusService
{
    public string ServiceNumber { get; set; }
    public string BrandName { get; set; }
    public List<Location> Locations { get; set; }

    public BusService(string brandName, string serviceNumber)
    {
        BrandName = brandName;
        ServiceNumber = serviceNumber; 
        Locations = new List<Location>();
    }
}

XAML 代码:

<StackPanel x:Name="ServiceStack">
    <ItemsControl x:Name="lstMachineFunctions">
        <ItemsControl.ItemTemplate  >
            <DataTemplate>   
                <Grid HorizontalAlignment="Stretch">
                    <usercontrols:BusServiceCard/>
                     <Button Tag="{Binding ServiceNumber}" Background="{Binding Brands[BrandName]}" Height="50" Click="ButtonCl"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

正如您从 XAML 中看到的,我当前的尝试一直在尝试 Background="{Binding Brands[BrandName]}" 但这没有奏效,任何帮助将不胜感激。

最佳答案

您可以使用 IValueConverter 来执行此操作。

public class BrandColorConverter : IValueConverter
{
    public Dictionary<String, Brush> Brands = new Dictionary<string, Brush>()
    {
        { "brand1", Brushes.Red },
        { "brand2", Brushes.Blue }
    };

    public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
    {
        if (!(value is BusService))
            return Binding.DoNothing;

        var busService = (BusService)value;

        if (!Brands.ContainsKey(busService.BrandName))
            return Binding.DoNothing;

        return Brands[busService.BrandName];
    }

    public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

在xaml中,将其添加为静态资源:

<Window.Resources>
    <local:BrandColorConverter x:Key="BrandColorConverter"/>
</Window.Resources>

并在您的按钮中使用它:

  <Button Tag="{Binding ServiceNumber}" 
     Background="{Binding Converter={StaticResource BrandColorConverter}}" 
     Height="50" 
     Click="ButtonCl"/>

此绑定(bind)转到当前元素,因此整个 BusService 对象将传递给转换器。

希望它能解决您的问题。

如果您打算将 WPF 与数据绑定(bind)一起使用,我强烈建议您研究 MVVM 模式,因为它使事情变得更加简化。

关于c# - 将字典值 DataBind 到 ObservableCollection C# - XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51116125/

相关文章:

c# - 在windows项目中调用asp.net webmethod

c# - 如何访问在ashx中修改的aspx中的 session ?

c# - C# 中静态只读变量和集合的奇怪行为

c# - 如何在 C# 的集合中保存不同类型的对象?

wpf - 为什么我的GridSplitter根本不工作?

c# - 如何解析从文件加载的 soap 消息?

wpf - Windows 服务与 WPF 管理工具

c# - 运算符 '|' 不能应用于类型 'System.Enum' 和 'System.Enum' 的操作数

c# - 基于枚举的 XAML - Xamarin 表单

c# - 在 WPF ListView 中逐项滚动