【发布时间】:2014-08-19 14:28:37
【问题描述】:
我有一个通过 LINQ 绑定到数据源的 itemscontrol。我正在尝试创建一个可重复的控件,它 a) 显示从 LINQ 查询返回的记录,b) 允许用户添加新行。
我有 2 个收藏:
a) fareItemCollection 这包含 fareItem 对象的集合,其中包括 fareDate 和 PermitNumber。
b) permitNumbers 这是一个 Ienumerable
我可以在可重复的 itemsControl 中显示票价项目。我希望能够将许可证编号显示为 permitNumbers 的下拉列表(即组合框),并选择该票价的 permitNumber。如果他们愿意,用户应该能够选择一个不同的许可证号来分配给该 fareItem。
我通过 LINQ 检索所需数据没有问题,这就是我在我正在努力解决的组合框中绑定 permitNumbers 数据的方式。我了解如何将数据集正常绑定到组合框,但当它位于绑定到另一个源的项目控件中时则不然。这甚至可能吗,还是有其他方法可以解决这个问题?
到目前为止,这是我的 xaml - 注意 PermitNumbers 是我的 iEnumerable 的名称,它是窗口类中的公共属性:
<ItemsControl Name="fareItemsControl" ItemsSource="{Binding}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0">
<TextBlock>Date</TextBlock>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="0">
<TextBlock>Driver</TextBlock>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="1">
<TextBlock Text="{Binding FareDate, StringFormat={}\{0:dd/MM/yy\}, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource FormFieldTextBoxStyle}">
</TextBlock>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="1">
<ComboBox ItemsSource="{Binding Path=PermitNumbers, UpdateSourceTrigger=PropertyChanged}" IsEditable="True"></ComboBox>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Style.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0">
<TextBlock>Date</TextBlock>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="0">
<TextBlock>Driver</TextBlock>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="1">
<DatePicker SelectedDate="{Binding FareDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource datePickerStyle}"> </DatePicker>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="1">
<ComboBox ItemsSource="{Binding Path=PermitNumbers, UpdateSourceTrigger=PropertyChanged}" IsEditable="True"></ComboBox>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
谢谢 凯
更多信息: 以下是我在绑定到上述 fareItemsControl 的 fareObjectsCollection 中使用的 fareObject:
public class FareProxy
{
DateTime _fareDate;
public DateTime FareDate
{
get
{
return _fareDate;
}
set
{
_fareDate = value;
}
}
IEnumerable<string> _permitNumbers;
public IEnumerable<string> PermitNumbers
{
get
{
return _permitNumbers;
}
set
{
_permitNumbers = value;
}
}
}
对于每个票价项目,日期会在项目控件中正确显示,但 permitNumbers 不会绑定到 permitNumbers 组合框。我尝试在我正在使用的窗口的类中创建一个 permitNumbers 属性,并通过将组合框的源路径设置为 permitNumbers 属性来加载 permitNumbers,如下面的帖子中所建议的,但这也不起作用。
【问题讨论】:
-
这是我如何在我正在努力解决的组合框中绑定 permitNumbers 数据的方式...愿意给我们更好地描述您的问题吗?我不知道你想要什么。
-
@Sheridan 道歉 Sheridan - 这很复杂 - 我试图在上面为您提供更多信息。基本上,如果我只有一个组合框,我想将 permitNumbers 集合绑定到我会很好,但因为组合框位于绑定到不同源的 itemsControl 内,我无法将 permitNumbers 绑定到它。
-
是否有可能有一个绑定的 itemsControl,它在也是 dataBound 的组合框中包含一个下拉列表的值?
-
好的 - 我刚刚发现我可以创建一个新的 fareObject 并将其添加到 fareItemCollection 并绑定 fareItemCollection,如 @Sheridan 所示。如果有人对更好的方法有任何建议,请随时发表评论。我现在对这个问题有点盲目,所以可能没有想出最好的解决方案!感谢你的帮助。凯