简体   繁体   中英

Binding items in ListBox in WP8

In a Windows Phone 8 app, I have a listbox with 2 TextBlocks and a button. I have a list of 2 strings and a boolean & I am able to bind the strings to the TextBlocks.

<ListBox Name="ListboxTest">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Key}" TextWrapping="Wrap"/>
            <TextBlock Text="{Binding Value}" TextWrapping="Wrap"/>
            <Button />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And this is the C# code to bind to the list box.

public class Detail
{
    public string Key { get; set; }
    public string Value { get; set; }
    public bool check { get; set; }
}
public List<Detail> ClassList = new List<Detail>();
ListboxTest.ItemsSource = ClassList;

I want to display the button only when the boolean value is true. How do I do it?

Take a look at this . Actually what you really need is a Converter by implementing the IValueConverter . This is also a good example where you could read about it. Bind the boolean value with the visibility property of the button and you are done! ;)

You can use boolean to visibility converter to hide, show button Here are example:

public class BoolToVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var boolValue = false;
            if (value != null) boolValue = (bool)value;
            return boolValue ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

In app.xaml

<my:BoolToVisibility x:Key="BoolToVisibility"/>

In your data template

<Button Visibility="{Binding Path=YourBoolProperty,Converter={StaticResource BoolToVisibility}}>

Please try those use Triggers.

Various Triggers in windows phone Msdn .

Please use ObservableCollection in WP8 for binding instead of List.

Please make your properties are implemented with INotifyPropertyChanged If your Boolean property is not implemented with inotifypropertychanged the view will not know the value is changed.hence the Data trigger will not work.

Namespace

  xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

  xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
x:Class="XXX_XXXX"

<Button Content="My button"
               Stretch="None"
               HorizontalAlignment="Stretch" 
               VerticalAlignment="Top">

                                <interactivity:Interaction.Triggers>
                <ec:DataTrigger Binding="{Binding Check}" Value="True">
                    <ec:ChangePropertyAction PropertyName="Visibility">
                        <ec:ChangePropertyAction.Value>
                            <Visibility>Visible</Visibility>
                        </ec:ChangePropertyAction.Value>
                    </ec:ChangePropertyAction>
                </ec:DataTrigger>

                <ec:DataTrigger Binding="{Binding Check}" Value="False">
                    <ec:ChangePropertyAction PropertyName="Visibility">
                        <ec:ChangePropertyAction.Value>
                            <Visibility>Collapsed</Visibility>
                        </ec:ChangePropertyAction.Value>
                    </ec:ChangePropertyAction>
                </ec:DataTrigger>

            </interactivity:Interaction.Triggers>
        </Button>

Note Answered from phone syntax may not be correct

Or, you could add this property to the Detail class:

public Visibility ButtonVisibility {
    get {
        return this.check == true ? Visibility.Visible : Visibility.Collapsed;
    }
}

And then just bind the button's Visibility to the ButtonVisibility property without any converters.

<Button Visibility="{Binding ButtonVisibility}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM