绑定绑定itemscontrol wpf的空datatemplate的组合框值

时间:2014-08-19 14:28:37

标签: wpf xaml binding combobox

我有一个通过LINQ绑定到数据源的itemscontrol。我正在尝试创建一个可重复的控件,a)显示从LINQ查询返回的记录,b)允许用户添加新行。

我有两个系列:

a)fareItemCollection它包含一个fareItem对象的集合,其中包括fareDate和PermitNumber。

b)permitNumbers这是一个不可数的

我可以在可重复的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>

由于 凯


更多信息: 以下是我在fareObjectsCollection中使用的fareObject,它绑定到上面的fareItemsControl:

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属性,并通过将comboBox的源路径设置为permitNumbers属性来加载permitNumbers,如下面的帖子中所建议的那样,但这也没有用。

1 个答案:

答案 0 :(得分:0)

您需要进行一些更改。首先要做的是...... Window所在的UserControlfareItemsControl ItemsControl将对象设置为其DataContext ...我认为您刚刚设置了fareItemCollectionDataContext。那是你的第一个错误。

该集合属性属于某个类。将您的permitNumbers集合作为属性移动到同一个类。现在将该类的实例(包含两个集合属性)设置为DataContext

现在,您需要更新ItemsControl.ItemsSource媒体资源:

<ItemsControl Name="fareItemsControl" ItemsSource="{Binding fareItemCollection}" ... />

对于最后一部分,@ethicallogics指出您需要使用RelativeSource BindingpermitNumbers内部访问DataTemplate集合,这是正确的。但是,他省略了必要的DataContext部分。所以正确的RelativeSource Path应该是更像这样的东西:

<ComboBox ItemsSource="{Binding DataContext.permitNumbers, RelativeSource={
    RelativeSource AncestorType={x:Type Window}}}" IsEditable="True"></ComboBox>

这意味着ComboBox.ItemsSource正在对象中查找名为permitNumbers的属性,该属性设置为父DataContext的{​​{1}}。这应该可以解决问题。

相关问题