列表框isSelected数据绑定中的DataTemplate

列表框isSelected数据绑定中的DataTemplate

本文介绍了列表框isSelected数据绑定中的DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



XAML:

 < FlipView的ItemsSource ={绑定源= {StaticResource的itemsViewSource}}... > 
< FlipView.ItemTemplate>
<&DataTemplate的GT;
<用户控件加载=StartLayoutUpdates
空载=StopLayoutUpdates>
<! - 其他控制 - >
<列表框Grid.Row =1Grid.ColumnSpan =3
的SelectionMode =多VerticalAlignment =中心
的ItemsSource ={结合答案}>
< ListBox.Resources>
<局部:LogicToText X:键=logToText/>
< /ListBox.Resources>

<! - 绑定从
代码的一种方法只IsSelected内容 - >
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
< ListBoxItem的
IsSelected ={结合IsSelected,模式=双向,转换器= {StaticResource的logToText}}
含量={结合IsSelected,模式=双向,转换器= {StaticResource的logToText }}>

< / ListBoxItem的>

< / DataTemplate中>
< /ItemsControl.ItemTemplate>


<! - 不是在所有
将工作; ListBox.Resources>
<风格的TargetType =ListBoxItem的>
< setter属性=IsSelected
值={结合IsSelected,模式=双向}/>
< setter属性=内容
值={结合IsSelected,模式=双向}/>
< /样式和GT;
< /ListBox.Resources> - >

< /列表框>
< /用户控件>
< / DataTemplate中>
< /FlipView.ItemTemplate>
< / FlipView>



代码:



答案

 私人的ObservableCollection< PrawoJazdyDataAnswer> _answers = 
新的ObservableCollection< PrawoJazdyDataAnswer>();
公众的ObservableCollection< PrawoJazdyDataAnswer>答案
{
得到
{
返回this._answers;
}
}



单项(答案)

 公共类PrawoJazdyDataAnswer:// NPCHelper PrawoJazdy.Common.BindableBase 
{
公共PrawoJazdyDataAnswer(ANS字符串,布尔ANSB)
{
this._ans = ANS;
this._isSelected = ANSB;
}

公共重写字符串的ToString()
{
返回_isSelected.ToString(); //只为了调试
//正常返回_ans
}
私人字符串_ans;
公共字符串答
{
{返回this._ans; }
//设置{this.SetProperty(REF this._ans,价值); }
}

私人布尔_isSelected;
公共BOOL IsSelected
{
{返回this._isSelected; }

{
_isSelected =价值;
FirePropertyChanged(IsSelected);
//this.SetProperty(ref this._isSelected,价值);
}
}
}



FirePropertyChanged

 公共类NPCHelper:INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效FirePropertyChanged(字符串道具)
{
如果(的PropertyChanged!= NULL)
的PropertyChanged(这一点,新PropertyChangedEventArgs(丙));
}
}



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

 公共类LogicToText:的IValueConverter 
$ { b $ b ///<总结>
///
///< /总结>
公共对象转换(对象的值,类型TARGETTYPE,
对象参数,字符串语言)
{
//如果(价值== NULL ||(布尔)值==假)
//返回假;

返回value.ToString();
}

///<总结>
///
///< /总结>
公共对象ConvertBack(对象的值,类型TARGETTYPE,
对象参数,字符串语言)
{
返回value.ToString()。包含(真)?真假;
}

提前

谢谢,我的英语水平(仍然在学习)抱歉。

@edit
感谢您的快速答复



为了测试我创建了一个按钮,文本块:

 <按钮点击=SPR>Sprawdź< /按钮> 
< TextBlock的文本={结合答案[0] .IsSelected,模式=双向}> < / TextBlock的>



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

 私人无效SPR(对象发件人,RoutedEventArgs E)
{
VAR ANS =((PrawoJazdyDataQuestion)this.flipView.SelectedItem).Answers;
的foreach
item.IsSelected = item.IsSelected(ANS中VAR项目)?假:真实的;
}



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



@ EDIT2固定错别字......


解决方案

不要绑定到IsSelected上ListBoxItem的。 ListBox中得到的SelectedItem或SelectedItems财产,你应该通过项目(S),以通过结合来选择,即。
模型设置IsSelected属性是不是好主意。最好做的SelectedItem在视图模型通知属性。


I try to simply databind IsSelected property with IsSelected field in my class. But after I change the value in code, it doesn't change the property, neither does clicking on ListBoxItem change the field value.

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>

Code:

Answers

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

Single item(Answer)

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));
    }
}

Converter(which sometimes seems to be needed and others not..., I tried ~10 approaches from different tutorials/examples)

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;
    }

Thanks in advance, and sorry for my English(still learning).

@editThanks for quick reply.

For test purposes I created a button and text block:

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

It's in other controls part (above list box, but in FlipView)Click method

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;
    }

As I wrote, when i'm changing data from code side, it is changing content of element, but not appearance of ListBoxItem. And if I just select it on ListBox, it doesn't change the data in TextBlock neither in ListBox itself.

@edit2 fixing typos...

解决方案

Do not bind to IsSelected on ListBoxItem. ListBox got SelectedItem or SelectedItems property, where You should pass item(s) to be selected, i.e. by binding.Setting IsSelected property in model is not good idea. Better make SelectedItem notify property in ViewModel.

这篇关于列表框isSelected数据绑定中的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 07:56