无法获取,从包含在wpf MVVM中动态生成的radiobuttons的列表框中设置selectedItem

时间:2012-08-20 12:55:56

标签: c# wpf mvvm

我试图使用动态单选按钮填充列表框,这些按钮被定制为togglebuttons。如上所述,我可以用radiobuttons填充列表框项目。但是,一旦我们选择了任何radiobuttons,我就无法在调试时从viewmodel对象的radiobuttons列表中设置所选项目。

以下是我资源目录中的xaml代码

 <Style x:Key="ScreensList" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton
                               VerticalAlignment="Center" GroupName="{Binding RelativeSource={RelativeSource TemplatedParent}}" 
                                IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}">
                                    <RadioButton.Template>
                                        <ControlTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <ToggleButton  IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
                                                    <StackPanel Width="80" Height="60" Orientation="Vertical" HorizontalAlignment="Left" Margin="10,10,20,10">
                                                        <Image Source="Default.png" Height="40"></Image>
                                                        <TextBlock Text="{Binding Path=ScreenNumber}" FontSize="11"></TextBlock>
                                                    </StackPanel>
                                                </ToggleButton>
                                            </StackPanel>
                                        </ControlTemplate>
                                    </RadioButton.Template>
                                </RadioButton>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

以下是我在xaml页面中的xaml代码

 <ListBox x:Name="ScreensList" ItemsSource="{Binding ScreenCollection}" 
                SelectedItem="{Binding Path=ScreenManager}" 
                  Style="{StaticResource ScreensList}" Width="365">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel FlowDirection="LeftToRight"  Orientation="Horizontal" >
                    </WrapPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

以下是viewmodel.cs

   public ObservableCollection<ScreensManager> ScreenCollection { get; set; }



    private ScreensManager _screenManager;

    public ScreensManager ScreenManager
    {
        get { return _screenManager; }
        set
        {
            if (_screenManager != value)
            {
                if (_screenManager != null)
                {
                    _screenManager = value;


                }
            }
        }
    }

 private void AddScreens()
    {
        int screenCount = Screen.AllScreens.Length;

        if (ScreenCollection == null)
            ScreenCollection = new ObservableCollection<ScreensManager>();
        for (int screenCounter = 1; screenCounter <= screenCount; screenCounter++)
        {
           if (screenCounter == 1)
           { 
            _screenManager = new ScreensManager();
            _screenManager.ScreenNumber = screenCounter;
            ScreenCollection.Add(_screenManager);
           }
        }
    }

以下是我的ScreenManager.cs模型类文件中的代码

 public ScreensManager()
   {

   }

   private int _screenNumber;
   public int ScreenNumber
   {
       get { return _screenNumber; }
       set
       {
           _screenNumber = value;
           OnPropertyChanged("ScreenNumber");
       }
   }

   private bool _selectedScreen;
   public bool SelectedScreen
   {
       get { return _selectedScreen; }
       set
       {
           if (_selectedScreen = value)
           {
               _selectedScreen = value;
               if (_selectedScreen != value)
               {
                   OnPropertyChanged("SelectedScreen");

               }
           }
       }
   }


    private void OnPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    public event PropertyChangedEventHandler PropertyChanged;

我无法找到我实际出错的地方,因为mvvm是全新的,有人请帮我解决我的问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您的SelectedScreen类型为bool,且类型为ScreensManager

您的ListBox.ItemsSource绑定了ObservableCollection<ScreensManager>,这意味着您的ListBox包含ScreensManager个对象的集合,但您的SelectedItem类型为bool }}。 bool对象永远不会等于ScreensManager对象,因此WPF不会选择任何内容,因为SelectedItem中找不到ItemsSource

SelectedScreen类型更改为ScreensManager而不是bool,并确保它等于ScreenCollection中存在的项目。 WPF按引用比较对象,而不是值,所以

ScreenManager.SelectedScreen = ScreenCollection.FirstOrDefault(); // Works
ScreenManager.SelectedScreen = new ScreensManager() { ... }; // Won't work

答案 1 :(得分:0)

确保绑定处于双向模式:

SelectedItem="{Binding Path=ScreenManager.SelectedScreen, Mode=TwoWay}"