【问题标题】:ListBox SelectedItem Binding BrokenListBox SelectedItem 绑定损坏
【发布时间】:2012-08-26 19:13:24
【问题描述】:

我正在使用WPF,遇到RadListBox SelectedItem 数据绑定的奇怪问题,试图弄清楚但没有运气。以下是我的场景

  1. 我正在使用 Telerik 控件(RadListBox 和 RadButton)
  2. RadButton 被放置在 ItemsControl 中,RadListBox 和 ItemsControl 绑定到相同的 ItemsSource。
  3. 我正在使用PRISM 和MVVM。
  4. 我想要的是当我点击按钮时,自动从RadListBox中选择相同的项目,(这部分工作正常)。
  5. 问题:只要我点击RadListBox 的任何项目,然后再点击任何按钮,项目选择就会停止工作。
  6. 编辑:我尝试使用标准 WPF ListBox 进行相同的操作,方法是添加选择更改事件的附加行为以及 Command 和 CommandParameter 的附加属性,它工作正常,所以它看起来像是 Telerik RadListBox 的问题?

现在让我来写代码。

ViewModel 类

public class MainViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public DelegateCommand<object> StudentSelected { get; set; }
    public DelegateCommand<object> ButtonPressed { get; set; }

    private void OnStudentSelected(object par)
    {
        //Debugger.Break();
        if (handled == false)
        {
            Student std = par as Student;
            if (std != null)
            {
                SelectedStudent = std;
            }
        }
        handled = false;
    }

    private void OnButtonPressed(object par)
    {
        //Debugger.Break();
        handled = true;
        String std = par as String;
        if (std != null)
        {
            foreach (Student st in _students)
            {
                if (st.Name.Equals(std))
                {
                    SelectedStudent = st;
                    break;
                }
            }
        }
    }    

    private Student _selectedstudent;
    private bool handled = false;


    public MainViewModel()
    {
        StudentSelected = new DelegateCommand<object>(OnStudentSelected);
        ButtonPressed = new DelegateCommand<object>(OnButtonPressed);
    }

    public Student SelectedStudent
    {
        get
        {
            return _selectedstudent;
        }
        set
        {
            _selectedstudent = value;
            OnPropertyChanged("SelectedStudent");
        }
    }


    private ObservableCollection<Student> _students;
    public ObservableCollection<Student> Students
    {
        get
        {
            return _students;
        }
        set
        {
            _students = value;
            OnPropertyChanged("Students");
        }
    }
}

public class Student
{
    public String Name { get; set; }
    public String School { get; set; }
}

MainView XAML

    <telerik:RadListBox Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Students}" Command="{Binding StudentSelected}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=SelectedItem}" SelectedItem="{Binding SelectedStudent, Converter={StaticResource DebugConverter}}">
<!-- The above debug converter is just for testing binding, as long as I keep on clicking button the Converter is being called, but the moment I click on RadListBoxItem the Converter is not called anymore, even when I click back on buttons -->
                <telerik:RadListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </DataTemplate>
                </telerik:RadListBox.ItemTemplate>
            </telerik:RadListBox>
            <Label Grid.Row="0" Grid.Column="1" Content="{Binding SelectedStudent.Name}"></Label>
            <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal">
                <ItemsControl ItemsSource="{Binding Students}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <telerik:RadButton Width="100" Height="70" Content="{Binding Name}" Command="{Binding RelativeSource={RelativeSource FindAncestor, 
    AncestorType={x:Type Window}}, Path=DataContext.ButtonPressed}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}">
                        </telerik:RadButton>
                    </DataTemplate>    
                    </ItemsControl.ItemTemplate>

                </ItemsControl>
            </StackPanel>

最后填充 ViewModel 并设置 Datacontext

MainViewModel mvm = new MainViewModel();
            ObservableCollection<Student> students = new ObservableCollection<Student>();
            students.Add(new Student { Name = "Student 1", School = "Student 1 School" });
            students.Add(new Student { Name = "Student 2", School = "Student 2 School" });
            students.Add(new Student { Name = "Student 3", School = "Student 3 School" });

            mvm.Students = students;

            //Bind datacontext
            this.DataContext = mvm;

请提供您的建议并分享您从 WPF 行话中获得的专业知识?

【问题讨论】:

    标签: wpf data-binding mvvm telerik prism


    【解决方案1】:

    我终于找到了问题所在,我只需要将 RadListBox SelectedItem 绑定替换为 TwoWay

    <telerik:RadListBox Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Students}" Command="{Binding StudentSelected}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=SelectedItem}" SelectedItem="{Binding SelectedStudent, Mode,TwoWay, Converter={StaticResource DebugConverter}}">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 2011-01-02
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多