【问题标题】:Binding the combobox values for an empty datatemplate of a bound itemscontrol wpf为绑定项目控件 wpf 的空数据模板绑定组合框值
【发布时间】: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 所示。如果有人对更好的方法有任何建议,请随时发表评论。我现在对这个问题有点盲目,所以可能没有想出最好的解决方案!感谢你的帮助。凯

标签: wpf xaml binding combobox


【解决方案1】:

您需要进行一些更改。首先要做的事情...无论您的fareItemsControl ItemsControl所在的WindowUserControl都有一个对象设置为其DataContext...我认为您刚刚将fareItemCollection设置为DataContext .这是你的第一个错误。

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

现在,您需要更新您的 ItemsControl.ItemsSource 属性:

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

对于最后一部分,@ethicallogics 指出您需要使用RelativeSource BindingDataTemplate 内部访问您的permitNumbers 集合是正确的。但是,他省略了基本的DataContext 部分。所以正确的RelativeSource Path 应该是这样的:

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

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

【讨论】:

  • 谢谢 - 当我的 fareItemCollection 中有一个 fareObject 时,这非常有效。我遇到的问题是当我的 fareObjectCollection 为空时,我想允许我的用户向集合中添加一个新的 fareObject。我希望他们能够从组合框中选择一个 permitnumber,其中 permitNumbers 是从数据源绑定的。这是我的问题,不确定如何将该 permitNumbers 集合绑定到 itemsControl 中的空行。认为在向集合中添加新项目时我可能会以错误的方式来处理它......
  • 在 Stack Overflow,我们通常在一个问题中问一个问题。由于您最初的问题现已得到解答,请mark this question as accepted 并针对您的新问题提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2022-12-06
相关资源
最近更新 更多