列表框isSelected数据绑定中的DataTemplate(listbox isSelected

2019-09-22 12:52发布

我尽量简单数据绑定IsSelected属性与IsSelected场在我的课。 但我改变代码的值之后,它不会改变的财产,既没有点击ListBoxItem更改字段值。

XAML:

<FlipView ItemsSource="{Binding Source={StaticResource itemsViewSource}}" ... >
    <FlipView.ItemTemplate>
        <DataTemplate>
            <UserControl Loaded="StartLayoutUpdates" 
                Unloaded="StopLayoutUpdates">
                <!-- other controls -->
                <ListBox Grid.Row="1" Grid.ColumnSpan="3"
                    SelectionMode="Multiple" VerticalAlignment="Center" 
                    ItemsSource="{Binding Answers}">
                    <ListBox.Resources>
                        <local:LogicToText x:Key="logToText" />
                    </ListBox.Resources>

                     <!-- bind IsSelected only in one way from 
                         code to content --> 
                     <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <ListBoxItem 
                              IsSelected="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource logToText}}" 
                              Content="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource logToText}}">

                            </ListBoxItem>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>


                    <!-- not working at all
                    <ListBox.Resources>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="IsSelected" 
                                Value="{Binding IsSelected, Mode=TwoWay}"/>
                            <Setter Property="Content" 
                                Value="{Binding IsSelected, Mode=TwoWay}"/>
                        </Style>
                    </ListBox.Resources>-->

                </ListBox>
            </UserControl>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

码:

答案

private ObservableCollection<PrawoJazdyDataAnswer> _answers = 
    new ObservableCollection<PrawoJazdyDataAnswer>();
public ObservableCollection<PrawoJazdyDataAnswer> Answers 
{ 
    get 
    { 
       return this._answers; 
    }  
}    

单项(答案)

public class PrawoJazdyDataAnswer : NPCHelper// PrawoJazdy.Common.BindableBase
{
    public PrawoJazdyDataAnswer(String ans, bool ansb)
    {
        this._ans = ans;
        this._isSelected = ansb;
    }

    public override string ToString() 
    { 
        return _isSelected.ToString();  //Only For debug purposes 
                                        //normally return _ans 
    }
    private string _ans;
    public string Ans
    {
        get { return this._ans; }
        //set { this.SetProperty(ref this._ans, value); }
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get { return this._isSelected; }
        set
        {
            _isSelected = value;
            FirePropertyChanged("IsSelected");
            //this.SetProperty(ref this._isSelected, value); 
        }
    }
}

FirePropertyChanged

public class NPCHelper : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void FirePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

转换器(有时似乎有需要和其他人不......,我试着从〜不同的教程/例子10点的方法)

public class LogicToText : IValueConverter
{
    /// <summary>
    /// 
    /// </summary>
    public object Convert(object value, Type targetType, 
                          object parameter, string language)
    {
        //if (value == null || (bool)value == false)
          //  return "False";

        return value.ToString();
    }

    /// <summary>
    /// 
    /// </summary>
    public object ConvertBack(object value, Type targetType, 
                              object parameter, string language)
    {
        return value.ToString().Contains("True") ? true : false;
    }

在此先感谢,并为我的英语很抱歉(仍在学习)。

@edit感谢快速回复。

出于测试目的,我创建了一个按钮和文本块:

<Button Click="spr" >Sprawdź</Button>
<TextBlock Text="{Binding Answers[0].IsSelected, Mode=TwoWay}" > </TextBlock> 

这是一个在其他控件部分(上面列表框,但在FlipView )Click方法

private void spr(object sender, RoutedEventArgs e)
    {
        var ans = ((PrawoJazdyDataQuestion)this.flipView.SelectedItem).Answers;
        foreach (var item in ans)
            item.IsSelected = item.IsSelected ? false : true;
    }

正如我写的,当我从代码中端的数据变化,它正在改变元素的内容,而不是外观ListBoxItem 。 如果我选择它的ListBox ,它不改变数据TextBlock没有在ListBox本身。

@ EDIT2固定错别字...

Answer 1:

不要绑定到IsSelected上ListBoxItem的。 ListBox中得到的SelectedItem或selectedItems属性中,你应该通过项目(S)被选中,通过结合IE浏览器。 在模式设置IsSelected属性是不是好主意。 最好做的SelectedItem在视图模型通知物业。



Answer 2:

要改变IsSelected的财产ListBoxItem ,您需要更改ListBox.ItemContainerStyle 。 看这里:

<ListBox Grid.Row="1" Grid.ColumnSpan="3"
                SelectionMode="Multiple" VerticalAlignment="Center" 
                ItemsSource="{Binding Answers}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding IsSelected}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

由于装订模式是TwoWay ,选择和取消选择ListBoxItem的动态改变的项目的内容,以显示无论是TrueFalse

还要注意我如何改变ListBox.ItemTemplateTextBlock ,而不是一个ListBoxItem 。 所述ItemTemplate定义的内容ListBoxItem ,所以某些类型的内容的控制,通常使用。 下面是对于不同的布局的用户界面结构(这可以通过使用WPF树可视化查看)。

ListBoxItemItemTemplate

TextBlockItemTemplate

编辑

另外请注意,我删除了IValueConverter 。 由于您的源和目标属性都是bool ,没有必要在这种情况下的转换器。 尝试删除该转换器的引用,看看是否能解决问题。



文章来源: listbox isSelected databinding in DataTemplate