简体   繁体   English

绑定字符串和布尔在ListView中?

[英]Bind string and bool in a ListView?

I have a ListView that contains a TextBlock and a Checkbox. 我有一个包含一个TextBlock和一个复选框的ListView。

I have bound the TextBlock to a String: 我已经将TextBlock绑定到一个字符串:

<DataTemplate x:DataType="x:String">
//Stuff
        <TextBlock Text="{x:Bind}" x:Name="Text">

Now I also want to have a Checkbox that can be toggled and bound to a List. 现在,我还想拥有一个可以切换并绑定到列表的复选框。 I couldn't manage to bind a bool to it. 我无法将布尔值绑定到它。 My current way is: I toggle the box and I can get the Parent, find something called "Text", get it's Text, look up the Index of it in my ObservableCollection that is bound and it's also the index of my checkbox! 我当前的方式是:切换该框,我可以获取父级,找到一个名为“文本”的东西,获取它的文本,在绑定的ObservableCollection中查找它的索引,这也是我的复选框的索引! That works, but Binding would work a lot nicer. 可以,但是Binding会更好。 Is there a way to do it with binding?? 有没有办法用绑定做到这一点?

It is so easy and you can write a converter to convert the string to bool. 它是如此简单,您可以编写一个转换器将字符串转换为bool。

public class StringToBoolConvert : IValueConverter
{
    public string TrueString { get; set; } = "true";

    /// <inheritdoc />
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (TrueString.Equals(value?.ToString()))
        {
            return true;
        }

        return false;
    }

    /// <inheritdoc />
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotSupportedException();
    }
}

And then you can add the StringToBoolConvert to the resource. 然后,您可以将StringToBoolConvert添加到资源。

<Page.Resources>
    <local:StringToBoolConvert x:Key="StringToBoolConvert" TrueString="1"></local:StringToBoolConvert>
</Page.Resources>

You can use the StringToBoolConvert in the DataTemplate. 您可以在DataTemplate中使用StringToBoolConvert。

    <ListView ItemsSource="{x:Bind List}">
        <ListView.ItemTemplate >
            <DataTemplate x:DataType="x:String">
                <Grid>
                    <StackPanel>
                        <TextBlock x:Name="Text" Text="{x:Bind}" ></TextBlock>
                        <CheckBox IsChecked="{x:Bind Converter={StaticResource StringToBoolConvert}}"></CheckBox>
                    </StackPanel>

                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

We write some code to bind in ListView. 我们编写一些代码绑定到ListView中。

    public MainPage()
    {
        this.InitializeComponent();
        DataContext = this;
    }

    public ObservableCollection<string> List { get; set; } = new ObservableCollection<string>()
    {
        "1", "2", "3"
    };

And we use the StringToBoolConvert and set the TrueString where the value is true and when we check the CheckBox. 然后,我们使用StringToBoolConvert并将TrueString设置为true时以及检查CheckBox时。

For we set the TrueString to "1" and we will check the CheckBox when the value is "1". 因为我们将TrueString设置为“ 1”,并且当值是“ 1”时,我们将检查CheckBox。 Try to run the code and you will view this image. 尝试运行代码,您将看到此图像。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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