【问题标题】:Change item checkbox on ListBox selection change更改列表框选择更改上的项目复选框
【发布时间】:2014-01-16 10:39:09
【问题描述】:

我有一个由 ItemTemplate 和 Binding 创建的 ListBox

        <controls:PanoramaItem Header="{Binding AppResources.SettingsSubpage2, Source={StaticResource LocalizedStrings}}" HeaderTemplate="{StaticResource HeaderTemplate}">
            <Grid>
                <ListBox x:Name="DayOfWeekSelector" ItemTemplate="{StaticResource DayOfWeekTemplate}" ItemsSource="{Binding DayOfWeekElementList}" Foreground="{StaticResource AppForegroundColor}" LostFocus="DayOfWeekSelector_LostFocus" HorizontalAlignment="Left" Width="420" />
            </Grid>
        </controls:PanoramaItem>

模板代码:

<phone:PhoneApplicationPage.Resources>
   <!--- ... --->
<DataTemplate x:Key="DayOfWeekTemplate">
        <Grid Height="65" Width="332">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}" Tag="{Binding}" d:LayoutOverrides="Width, Height" BorderBrush="{StaticResource AppBackgroundColor}" Background="{StaticResource ScheduleBackgroundAccentsColor}" Grid.Column="0" Unchecked="CheckBox_Unchecked" />
            <StackPanel Grid.Column="1" Orientation="Horizontal">
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center"  d:LayoutOverrides="Width"/>
                <TextBlock Text="{Binding TaskCounter, Mode=OneWay, Converter={StaticResource DayOfWeekCounter}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"/>
            </StackPanel>
        </Grid>
    </DataTemplate>
   <!--- ... --->

而且它运行良好。我的所有物品都在清单上。复选框绑定到适当的元素(单击它会更改适当的值)。

但默认情况下也可以选择 ListBox。选择高亮 TextBox 绑定到 Name 但不更改 CheckBox(绑定到 IsActive)。如何将项目选择更改绑定到复选框状态更改(在 Silverlight 中)?

编辑:

public partial class SettingsPage : PhoneApplicationPage, INotifyPropertyChanged
{

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

    public List<DayOfWeekElement> DayOfWeekList
    {
        get
        {
            return CyberSyncPlanBase.Instance.DayOfWeekElementList;
        }
        set
        {
            CyberSyncPlanBase.Instance.DayOfWeekElementList = value;
            NotifyPropertyChanged("DayOfWeekList");
        }
    }

   public SettingsPage()
    {
        InitializeComponent();
        DayOfWeekSelector.DataContext = CyberSyncPlanBase.Instance;

    }

 private void DayOfWeekSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DayOfWeekElement dowe = (DayOfWeekElement) DayOfWeekSelector.SelectedItem;

        if (dowe != null)
            dowe.IsActive = (dowe.IsActive) ? false : true;
    }

在单例 INotifyPropertyChanged 中,我以同样的方式实现:

    private List<DayOfWeekElement> dayOfWeekElementList;
    public List<DayOfWeekElement> DayOfWeekElementList 
    { 
        get { return dayOfWeekElementList; }
        set 
        { 
        dayOfWeekElementList = value;
        RecalcWeekTasks();
        NotifyPropertyChanged("DayOfWeekElementList");
        } 
    }

底层:

public class DayOfWeekElement
{
    public string Name { get { return this.DayOfWeek.ToStringValue(); } }
    public bool IsNotEmpty { get { return (TaskCounter > 0); } }
    public int TaskCounter { get; set; }
    public bool IsActive { get; set; }
    public DayOfWeek DayOfWeek { get; set; }
}

【问题讨论】:

    标签: c# silverlight templates checkbox listbox


    【解决方案1】:

    我认为你可以使用 ListBox 控件的SelectedItem 属性。

    一个可能的实现可能是这样的:

    1. 订阅ListBox的SelectedIndexChanged事件。
    2. 获取所选项目。
    3. 对于所选项目,将其IsActive 属性更改为true

    如果接口 INotifyPropertyChanged 在您的数据类中实现,则此方法有效。

    例如:

    public class DayOfWeekElement : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;
    
       private void NotifyPropertyChanged(string propertyName)
       {
           if (PropertyChanged != null)
           {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
           }
       }
    
    
       private bool isActive = false;
       public bool IsActive { 
          get
          {
              return this.isActive;
          } 
          set
          {
              if (value != this.isActive)
              {
                 this.isActive= value;
                 NotifyPropertyChanged("IsActive");
              }
          }  
       }
    }
    

    【讨论】:

    • 好的,但它不会改变我的复选框状态。 (但“isActive”会改变)。我该怎么做才能“刷新”密码箱状态? (您需要我提供更多代码吗?)
    • @interneo 我确信您在数据类中实现了INotifyPropertyChanged 接口。您是否在数据类中实现此接口并为您的属性调用相关事件?
    • @interneo 谢谢。该接口也必须在您的DayOfWeekElement 类中实现,并且必须为您的类的每个属性调用事件NotifyPropertyChanged。查看我提供给您的文档链接中的示例。
    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 2013-07-12
    • 2016-09-28
    • 1970-01-01
    • 2014-09-16
    • 2012-10-11
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多