none
mvvm数据绑定问题 RRS feed

  • 问题

  • 有一堆复选框,我记录了一个List<string>。我想让这些CheckBox的IsCheck绑定在List<string>上,但是在Converter的时候,

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)   怎么读取到CheckBox的Content。

    我想读到Content,判断在List<string>存在就return true。

    2014年7月9日 9:25

答案

  • 你好,

    >>怎么读取到CheckBox的Content。我想读到Content,判断在List<string>存在就return true

    你可以使用 MultiValueConverter, 运用 IMultiValueConverter接口传入。

    以下是我根据你的需求做的一个小示例:

    ViewModel, 包含你需要的List<string>:

    class MyViewModel
    {
            public List<string> MyStringOptions { get; set; }
            public MyViewModel()
            {
                MyStringOptions = new List<string> { "Item1", "Item4", "Item5" };
            }
    }

    View, 注意CheckBox.IsChecked中的 MultiBinding:

    <Window.DataContext>
            <vm:MyViewModel />
        </Window.DataContext>
        <Window.Resources>
            <cv:CheckBoxIsCheckedConverter x:Key="checkBoxIsCheckedConverter" />
        </Window.Resources>
        <StackPanel Orientation="Horizontal">
            <CheckBox Content="Item1" Height="20"  >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item2" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item3" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item4" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item5" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item6" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
        </StackPanel>

    Converter:

    public class CheckBoxIsCheckedConverter : IMultiValueConverter
    {
            public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string currentItem = values[0] as string; //当前CheckBox的Content
                List<string> stringOptions = values[1] as List<string>;
    
                var result = stringOptions.Find(s => s == currentItem); //使用LINQ语句查询
                if(result!=null) //查询到即返回True
                    return true;
                return false;
            }
    
            public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                return null;
            }
    }

    效果截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 huanghy 2014年7月14日 3:47
    2014年7月10日 9:44
    版主
  • 你好,

    这样是可以绑定成功,但是为什么自己点击的时候CheckBox外面会多一个红色的框框。

    请问要怎么去掉那个框框。

    你好,

    这个问题是因为 ConvertBack方法返回null,以下是完善后的转换器:

    public class CheckBoxIsCheckedConverter : IMultiValueConverter
    {
            public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string currentItem = values[0] as string;
                List<string> stringOptions = values[1] as List<string>;
                if (currentItem != null && stringOptions != null)
                { 
                    var result = stringOptions.Find(s => s == currentItem); //Find Item using LINQ
                    if(result != null)
                        return true;
                }
                return false;
            }
    
            public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                return new object[] { Binding.DoNothing, Binding.DoNothing };
            }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 huanghy 2014年7月14日 3:47
    2014年7月14日 1:02
    版主

全部回复

  • 你好,

    >>怎么读取到CheckBox的Content。我想读到Content,判断在List<string>存在就return true

    你可以使用 MultiValueConverter, 运用 IMultiValueConverter接口传入。

    以下是我根据你的需求做的一个小示例:

    ViewModel, 包含你需要的List<string>:

    class MyViewModel
    {
            public List<string> MyStringOptions { get; set; }
            public MyViewModel()
            {
                MyStringOptions = new List<string> { "Item1", "Item4", "Item5" };
            }
    }

    View, 注意CheckBox.IsChecked中的 MultiBinding:

    <Window.DataContext>
            <vm:MyViewModel />
        </Window.DataContext>
        <Window.Resources>
            <cv:CheckBoxIsCheckedConverter x:Key="checkBoxIsCheckedConverter" />
        </Window.Resources>
        <StackPanel Orientation="Horizontal">
            <CheckBox Content="Item1" Height="20"  >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item2" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item3" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item4" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item5" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
            <CheckBox Content="Item6" Height="20" >
                <CheckBox.IsChecked>
                    <MultiBinding Converter="{StaticResource checkBoxIsCheckedConverter}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
                        <Binding Path="MyStringOptions"/>
                    </MultiBinding>
                </CheckBox.IsChecked>
            </CheckBox>
        </StackPanel>

    Converter:

    public class CheckBoxIsCheckedConverter : IMultiValueConverter
    {
            public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string currentItem = values[0] as string; //当前CheckBox的Content
                List<string> stringOptions = values[1] as List<string>;
    
                var result = stringOptions.Find(s => s == currentItem); //使用LINQ语句查询
                if(result!=null) //查询到即返回True
                    return true;
                return false;
            }
    
            public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                return null;
            }
    }

    效果截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 huanghy 2014年7月14日 3:47
    2014年7月10日 9:44
    版主
  • 你好,

    这样是可以绑定成功,但是为什么自己点击的时候CheckBox外面会多一个红色的框框。

    请问要怎么去掉那个框框。

    2014年7月12日 1:42
  • 你好,

    这样是可以绑定成功,但是为什么自己点击的时候CheckBox外面会多一个红色的框框。

    请问要怎么去掉那个框框。

    你好,

    这个问题是因为 ConvertBack方法返回null,以下是完善后的转换器:

    public class CheckBoxIsCheckedConverter : IMultiValueConverter
    {
            public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string currentItem = values[0] as string;
                List<string> stringOptions = values[1] as List<string>;
                if (currentItem != null && stringOptions != null)
                { 
                    var result = stringOptions.Find(s => s == currentItem); //Find Item using LINQ
                    if(result != null)
                        return true;
                }
                return false;
            }
    
            public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                return new object[] { Binding.DoNothing, Binding.DoNothing };
            }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 huanghy 2014年7月14日 3:47
    2014年7月14日 1:02
    版主
  • 谢谢

    2014年7月14日 3:48