ItemsControl中的绑定未冒泡到ItemsSource中的项

本文关键字:ItemsSource 绑定 ItemsControl | 更新日期: 2023-09-27 18:24:26

以下是我在XAML中的代码:

        <!-- ItemsControl to print all the GDTs on the map as an overlay on tiles -->
        <ItemsControl ItemsSource="{Binding GDTs, Mode=OneWay}" Grid.ColumnSpan="3" Grid.RowSpan="3" Panel.ZIndex="7">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <Canvas ClipToBounds="True" SnapsToDevicePixels="true"/>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
              <Setter Property="Canvas.Left"  Value="{Binding DistanceToLeft}"/>
              <Setter Property="Canvas.Top"   Value="{Binding DistanceToTop}"/>
              <Setter Property="ContentTemplate">
                <Setter.Value>
                  <DataTemplate>
                    <Grid>
                      <userControls:CommIndicator CommConfig="eDt" DtAntennaMode="eDirectional" DtAzimuth="{Binding Yaw}"/>
                      <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding SourcePath}" Width="{Binding Width}"/>
                      <Rectangle Height="{Binding Height}" Width="{Binding Width}" Stroke="Orange" StrokeThickness="2"/>
                      <Ellipse Height="4" Width="4" Fill="Orange" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                  </DataTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </ItemsControl.ItemContainerStyle>
        </ItemsControl>

除了我刚刚在UserControl(CommIndicator)中添加的绑定外,其中的所有绑定都在工作:DtAzimuth="{Binding Yaw}"

我在Snoop中看到的关于该绑定的错误如下:"System.Windows.Data错误:40:BindingExpression路径错误:在"object"CommonIndicator"(Name='')"上找不到"Yaw"属性。BindingExpression:path=Yaw;DataItem='CommonIndicator'(Name=''');目标元素为"CommIndicator’(Name=''');目标属性为"DtAzimuth"(类型'Int32')"

有没有办法强制绑定签入ItemsControl"currentItem"?

编辑1:这是我的UserControl:的XAML

    <UserControl x:Class="UserControls.CommIndicator"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
             xmlns:userControls="clr-namespace:UserControls"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             SnapsToDevicePixels="True">
  <UserControl.Resources>
    <ResourceDictionary>
      <userControls:CommConfigToVisibility x:Key="CommConfigToVisibility"/>
      <userControls:AntennaModeToAngle     x:Key="AntennaModeToAngle"/>
      <userControls:AntennaModeToColor     x:Key="AntennaModeToColor"/>
    </ResourceDictionary>
  </UserControl.Resources>
  <Grid Width="100" Height="100">
        <!-- Only use this for design reference -->
        <!--<Grid Width="70" Height="70" HorizontalAlignment="Center" VerticalAlignment="Center">
          <Ellipse Width="66" Height="66" Stroke="WhiteSmoke" StrokeThickness="3" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <Ellipse Width="66" Height="66" Fill="WhiteSmoke" StrokeThickness="3" HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0.30"/>
          <Ellipse Width="70" Height="70" Stroke="LimeGreen" StrokeThickness="3" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="/Resources/BlackShadowTopView.png" Width="40"/>
          <TextBlock Text="160" Margin="6" HorizontalAlignment="Center" TextAlignment="Center" Foreground="Black"/>
        </Grid>-->
        <!-- ADR Communication Circle Indicator --> 
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="100"
              Visibility="{Binding CommConfig, Converter={StaticResource CommConfigToVisibility}, ConverterParameter={x:Static userControls:CommType.eDataRelay}}">
          <Ellipse Width="100" Height="100" Stroke="Black" StrokeThickness="7"/>
          <ed:Arc Width="99" Height="99" Fill="SlateGray" StartAngle="0" EndAngle="360" ArcThickness="5"/>
          <ed:Arc Width="99" Height="99" Stretch="None" ArcThickness="6"
                Fill="{Binding DrAntennaMode, Converter={StaticResource AntennaModeToColor}}"
                StartAngle="{Binding DrAntennaMode, Converter={StaticResource AntennaModeToAngle}, ConverterParameter={x:Static userControls:AngleType.eStartAngle}}" 
                EndAngle="{Binding DrAntennaMode, Converter={StaticResource AntennaModeToAngle}, ConverterParameter={x:Static userControls:AngleType.eEndAngle}}"/>
          <Grid.LayoutTransform>
            <RotateTransform Angle="{Binding DrAzimuth}"/>
          </Grid.LayoutTransform>
        </Grid>
        <!-- ADT/GDT Communication Circle Indicator -->
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="90" Height="90"
              Visibility="{Binding CommConfig, Converter={StaticResource CommConfigToVisibility}, ConverterParameter={x:Static userControls:CommType.eDataTransmitter}}">
          <Ellipse Width="88" Height="88" Stroke="Black" StrokeThickness="7"/>
          <ed:Arc Width="87" Height="87" Fill="SlateGray" StartAngle="0" EndAngle="360" ArcThickness="5"/>
          <ed:Arc Width="87" Height="87" Stretch="None" ArcThickness="6"
                Fill="{Binding DtAntennaMode, Converter={StaticResource AntennaModeToColor}}"
                StartAngle="{Binding DtAntennaMode, Converter={StaticResource AntennaModeToAngle}, ConverterParameter={x:Static userControls:AngleType.eStartAngle}}" 
                EndAngle="{Binding DtAntennaMode, Converter={StaticResource AntennaModeToAngle}, ConverterParameter={x:Static userControls:AngleType.eEndAngle}}"/>
          <Grid.LayoutTransform>
            <RotateTransform Angle="{Binding DtAzimuth}"/>
          </Grid.LayoutTransform>
        </Grid>
      </Grid>
    </UserControl>

ItemsControl中的绑定未冒泡到ItemsSource中的项

删除此:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

从UserControl,并添加以下内容:

<UserControl ...
             x:Name="view">

并将usecontrol中的所有绑定更改为使用ElementName,如下所示:

{Binding ElementName=view, Path=DtAntennaMode, Converter={StaticResource AntennaModeToColor}}"

此外,那里似乎有很多转换器。我强烈建议您将所有这些更改为使用ControlTemplate,并在其中放置ControlTemplate.Triggers,从而消除对转换器和DataContext黑客的需要。