0

这是我在 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' ''CommIndicator' (Name='')' 上找不到 'Yaw' 属性。BindingExpression :Path=Yaw; DataItem='CommIndicator' (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>
4

1 回答 1

1

删除这个:

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

来自 UserControl,并添加以下内容:

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

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

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

此外,那里似乎有很多转换器。我强烈建议您将所有这些更改为使用 aControlTemplate和 inside,您可以放置ControlTemplate.Triggers​​和删除对转换器和 DataContext hacks 的需求。

于 2013-07-23T20:29:19.493 回答