3

クラスのフィールドを使用して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)クリックメソッド

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のタイプミスの修正...

4

2 に答える 2

10

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、アイテムのコンテンツが動的に変更され、またはのいずれTrueかが表示されますFalse

ListBox.ItemTemplateまた、をではTextBlockなくに変更した方法にも注目してListBoxItemください。はのItemTemplateコンテンツを定義するListBoxItemため、通常、ある種のコンテンツコントロールが使用されます。以下は、さまざまなレイアウトのUI構造です(これは、WPFツリービジュアライザーを使用して表示できます)。

ListBoxItemとしてItemTemplate

ここに画像の説明を入力してください

TextBlockとしてItemTemplate

ここに画像の説明を入力してください

編集

また、を削除したことにも注意してくださいIValueConverter。ソースプロパティとターゲットプロパティはどちらもboolであるため、この場合はコンバーターは必要ありません。コンバーターの参照を削除して、問題が解決するかどうかを確認してください。

于 2012-09-18T13:05:19.337 に答える
1

ListBoxItemのIsSelectedにバインドしないでください。ListBoxはSelectedItemまたはSelectedItemsプロパティを取得しました。ここで、選択するアイテムを渡す必要があります。つまり、バインドします。モデルにIsSelectedプロパティを設定することはお勧めできません。SelectedItemがViewModelのプロパティに通知するようにすることをお勧めします。

于 2012-09-18T07:19:52.160 に答える