【问题标题】:DataBind a Dictionary value to ObservableCollection C# - XAML将字典值绑定到 ObservableCollection C# - XAML
【发布时间】:2018-12-09 11:57:25
【问题描述】:

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

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

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]}" 但是这没有奏效,非常感谢任何帮助。

【问题讨论】:

  • 它是一个复杂的画笔还是只是一个SolidColorBrush?是否可以在BusService 上添加Color
  • 它只是一个 SolidColourBrush,我不想向 BusService 类添加颜色属性,但是因为我希望能够更改品牌字典并更新所有巴士服务对象。
  • 制作一个环绕 BusService 的视图模型(例如 BusServiceViewModel),让它提供颜色

标签: c# wpf xaml dictionary binding


【解决方案1】:

您可以使用 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"/>

这个绑定到当前元素,所以整个 BusService 对象将被传递给转换器。

希望它能解决你的问题。

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

【讨论】:

  • 非常感谢,这只是诀窍,我一定会按照建议研究 MVVM。再次感谢。
  • IMO 这也是一个很好的解决方案,因为它将“为哪个小部件显示哪种颜色”的问题排除在视图模型之外。
猜你喜欢
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 2021-05-29
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多