无法将数据绑定到silverlight 4数据网格中的自动完成框

时间:2011-09-21 18:13:16

标签: silverlight

我是Silverlight的新手,我的任务是将datagrid TextColumns更改为自动完成框。我认为这应该相当简单,但显然不是。

我能够绑定数据网格外的列表中的数据,但不能从内部绑定。

我现在已经研究了两天,我发现的所有内容似乎都包含了数据库中的数据,或者对于我的新手大脑来说太复杂了。

我真正需要的只是一个简单的示例和解释,说明如何在数据网格中执行此操作而不是定期执行此操作。

我的代码如下。它成功构建但无法正常工作。

我确信这是许多其他人必须遇到的问题。

我感谢任何人的意见, 提前致谢。 d。

    <UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
         xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="AccordionAutoCompleteBox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="680" d:DesignWidth="1240">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
        <toolkit:AccordionItem x:Name="AccordionItem2" FontSize="12" Background="LightBlue" BorderBrush="Wheat" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsSelected="False" MaxHeight="400">
            <sdk:DataGrid Name="AccordionGrid"  ItemsSource="{Binding ExpData}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HeadersVisibility="All" BorderThickness="1" Margin="8" SelectionMode="Single" Canvas.ZIndex="-1" MaxHeight="360">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn Header="Exp">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <sdk:AutoCompleteBox x:Name="AutoCompGrid" Text="{Binding Exp, Mode=TwoWay}" ItemsSource="{Binding Exp}"  IsTextCompletionEnabled="True" />

                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Header="Exp" Binding="{Binding Exp}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Desc" Binding="{Binding Desc}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Prod" Binding="{Binding Prod}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Source" Binding="{Binding Source}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Start" Binding="{Binding Start}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Reset" Binding="{Binding Reset}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Amt" Binding="{Binding Amt}" IsReadOnly="False" />

                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
        </toolkit:AccordionItem>
        <sdk:AutoCompleteBox x:Name="AutoCompGrid2" Text="{Binding Exp}" ItemsSource="{Binding Exp}" IsTextCompletionEnabled="False" />
    </StackPanel>
</Grid>

背后的代码

    namespace AccordionAutoCompleteBox

{     public partial class MainPage:UserControl     {         公共MainPage()         {             的InitializeComponent();

        List<ExpData> myList = new List<ExpData>
        {
            new ExpData
            {Exp = "cell 1",
            Desc = "cell 2",
            Prod = "cell 3",
            Source = "cell 4",
            Start = "cell 5",
            Amt = "cell 6",
            Reset = "cell 7"},

            new ExpData
            {Exp = "cell 8",
            Desc = "cell 9",
            Prod = "cell 10",
            Source = "cell 11",
            Start = "cell 12",
            Amt = "cell 13",
            Reset = "cell 14"}
        };
        AccordionGrid.ItemsSource = myList;
        AutoCompGrid2.ItemsSource = myList;

        this.AccordionItem2.Header = " Accordion Header ";

    }
}
public class ExpData
{
    public String Exp { get; set; }
    public String Desc { get; set; }
    public String Prod { get; set; }
    public String Source { get; set; }
    public String Start { get; set; }
    public String Reset { get; set; }
    public String Amt { get; set; }

    public ExpData(String exp, string desc, string prod, string source, string start, String reset, String amt)
    {
        Exp = exp;
        Desc = desc;
        Prod = prod;
        Source = source;
        Start = start;
        Reset = reset;
        Amt = amt;
    }

    public override string ToString()
    {
        return Exp;
    }

1 个答案:

答案 0 :(得分:2)

如果你有一个视图模型,你的生活将会轻松得多。 :)使用ViewModel解决此问题的快速示例。

public class ExpDataViewModel
{
  private List<ExpData> _listData;

  public ExpDataViewModel()
  {
    _listData = new List<ExpData>
        {
            new ExpData
            {Exp = "cell 1",
            Desc = "cell 2",
            Prod = "cell 3",
            Source = "cell 4",
            Start = "cell 5",
            Amt = "cell 6",
            Reset = "cell 7"},

            new ExpData
            {Exp = "cell 8",
            Desc = "cell 9",
            Prod = "cell 10",
            Source = "cell 11",
            Start = "cell 12",
            Amt = "cell 13",
            Reset = "cell 14"}
        };
  }

  public IEnumerable<ExpData> ListData
  {
    get {return _listData;}
  }     

  public IEnumerable<string> ExpItems
  {
    get {return _listData.Select(i => i.Exp);
  }
}

然后你需要改变观点:

<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
         xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="AccordionAutoCompleteBox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="[Your ViewModel's Namespace]"
mc:Ignorable="d"
d:DesignHeight="680" d:DesignWidth="1240">
<UserControl.Resources>
  <vm:ExpDataViewModel x:Key="vm" />
</UserControl.Resource>

    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource vm}">
        <StackPanel VerticalAlignment="Center">
        <toolkit:AccordionItem x:Name="AccordionItem2" FontSize="12" Background="LightBlue" BorderBrush="Wheat" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsSelected="False" MaxHeight="400">
            <sdk:DataGrid Name="AccordionGrid"  ItemsSource="{Binding ListData}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HeadersVisibility="All" BorderThickness="1" Margin="8" SelectionMode="Single" Canvas.ZIndex="-1" MaxHeight="360">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn Header="Exp">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <sdk:AutoCompleteBox x:Name="AutoCompGrid" Text="{Binding Exp, Mode=TwoWay}" ItemsSource="{Binding Path=ExpItems, Source={StaticResource vm}}"  IsTextCompletionEnabled="True" />

                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Header="Exp" Binding="{Binding Exp}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Desc" Binding="{Binding Desc}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Prod" Binding="{Binding Prod}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Source" Binding="{Binding Source}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Start" Binding="{Binding Start}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Reset" Binding="{Binding Reset}" IsReadOnly="False" />
                    <sdk:DataGridTextColumn Header="Amt" Binding="{Binding Amt}" IsReadOnly="False" />

                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
        </toolkit:AccordionItem>
        <!-- not sure what this is supposed to be bound to --> 
        <sdk:AutoCompleteBox x:Name="AutoCompGrid2" Text="{Binding Exp}" ItemsSource="{Binding Exp}" IsTextCompletionEnabled="False" />
    </StackPanel>
</Grid>

您可以从代码隐藏文件中删除所有自定义代码。