Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CaliburnMicro-将项目列表绑定到ListBox_C#_Wpf_Mvvm_Listbox_Caliburn.micro - Fatal编程技术网

C# CaliburnMicro-将项目列表绑定到ListBox

C# CaliburnMicro-将项目列表绑定到ListBox,c#,wpf,mvvm,listbox,caliburn.micro,C#,Wpf,Mvvm,Listbox,Caliburn.micro,我需要创建一个列表,该列表绑定由名称、布尔状态和颜色指定的项 这是我的ViewModel: using Caliburn.Micro; public class MainWindowViewModel :Screen { private List<string> _commandListSource; private List<CommandItem> _commandsSource; public List<CommandItem>

我需要创建一个列表,该列表绑定由名称、布尔状态和颜色指定的项

这是我的ViewModel:

using Caliburn.Micro;

public class MainWindowViewModel :Screen
{
    private List<string> _commandListSource;
    private List<CommandItem> _commandsSource;

    public List<CommandItem> CommandsSource
    {
        get
        {
            return _commandsSource;
        }

        set
        {
            _commandsSource = value;
            NotifyOfPropertyChange(() => CommandsSource);
        }
    }

    public MainWindowViewModel()
    {
        _commandListSource = new List<string>();
        _commandListSource.Add("A");
        _commandListSource.Add("B");

        getsource();

        NotifyOfPropertyChange(() => CommandsSource);
    }
    private void getsource()
    {
        _commandsSource = new List<CommandItem>();
        foreach (var x in _commandListSource)
        {
            var ci = new CommandItem();
            ci.CommandName = x;
            ci.IsInTheOtherList = true;
            _commandsSource.Add(ci);
        }
    }
}
Xaml
列表框

<ListBox x:Name="Source" 
         ItemsSource="{Binding CommandsSource , NotifyOnSourceUpdated=True}" 
         HorizontalAlignment="Left"           
         ScrollViewer.VerticalScrollBarVisibility="Visible"
         VerticalAlignment="Stretch" MinWidth="100">
  <ListBox.ItemTemplate>
     <DataTemplate>
        <DockPanel >
           <TextBlock Text="*"/>
           <Ellipse Fill="{Binding BGColor}" Width="10" Height="10"/>
           <TextBlock Text="{Binding CommandName}"/>
        </DockPanel>
     </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>


问题是
列表框
仅显示
*
字符

我通过更改
命令项
类解决了这个问题:

public class CommandItem
{
    public string CommandName;
    public bool IsInTheOtherList;
    public Color BGColor
    {
        get
        {
            if (IsInTheOtherList)
                return Color.FromRgb(0, 0,255);
            return Color.FromRgb(255, 255,0);

        }
    }
}
public class CommandItemClass : PropertyChangedBase
{
    private string _commandName;
    private bool _isInTheOtherList;
    public SolidColorBrush BGColor
    {
        get
        {
            if (IsInTheOtherList)
                return new SolidColorBrush(Color.FromRgb(0, 0, 255));
            return new SolidColorBrush(Color.FromRgb(255, 255, 0));
        }
    }

    public string CommandName
    {
        get
        {
            return _commandName;
        }
        set
        {
            _commandName = value;
            NotifyOfPropertyChange(()=>CommandName);
        }
    }

    public bool IsInTheOtherList
    {
        get
        {
            return _isInTheOtherList;
        }
        set
        {
            _isInTheOtherList = value;
            NotifyOfPropertyChange(() => IsInTheOtherList);
            NotifyOfPropertyChange(()=>BGColor);
        }
    }
}