2010-04-19 95 views
12

我工作在一个大的WPF项目和调试我的输出窗口中充满了这些恼人的警告:如何摆脱恼人的Horizo​​ntalContentAlignment绑定警告?

System.Windows.Data信息:10:使用绑定和没有有效>回退值无法检索值存在;改为使用默认值。 BindingExpression:Path = Horizo​​ntalContentAlignment;的DataItem = NULL;目标元素是 'ComboBoxItem'(Name ='');目标属性是“Horizo​​ntalContentAlignment”(式>“ 的Horizo​​ntalAlignment”)

在特定示例中ComboBoxItem以这种方式称呼:

<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}"> 
    <Setter Property="OverridesDefaultStyle" Value="True"/> 
    <Setter Property="SnapsToDevicePixels" Value="True"/>     
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
       <Border 
        Name="bd" 
        Padding="4,4,4,4" 
        SnapsToDevicePixels="True" 
        CornerRadius="2,2,2,2"> 
        <ContentPresenter /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsHighlighted" Value="true"> 
         <Setter TargetName="bd" Property="Background" Value="{StaticResource MediumBrush}"/> 
         <Setter TargetName="bd" Property="Padding" Value="4,4,4,4"/> 
         <Setter TargetName="bd" Property="CornerRadius" Value="2,2,2,2"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我知道问题是由默认主题定义生成包含类似事情ComboBoxItem

<Setter Property="Control.HorizontalContentAlignment"> 
     <Setter.Value> 
      <Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" /> 
      </Setter.Value> 
     </Setter> 

但我也想到用

<Setter Property="OverridesDefaultStyle" Value="True"/> 

会照顾到问题,而是警告仍然存在。

编辑:为了重现您也需要重写组合框的完全一样,从MSDN在这个例子中所做的作风问题: ComboBox ControlTemplate Example

任何帮助,非常感谢

+0

我无法重现您的问题,这个XAML既不在4.0也不在3.5。它运行良好,没有任何绑定警告。 – majocha 2010-04-19 19:11:51

+0

你是对的,我单独测试,它不给我警告,我编辑问题的更多细节 – Drake 2010-04-20 09:25:43

+0

我看不到你在编辑链接的例子中有问题的绑定。 – majocha 2010-04-20 12:09:55

回答

1

我不不知道一年多后你是否仍然对这个问题感兴趣,但是我的解决方案是明确地写出这个值的风格。例如:

<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}"> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 

而且这只是解决了这个问题。

1

同样的问题是“菜单项”如果是直接放置在像“的StackPanel”等面板,并且可以使用上述卡特的回答是固定的,只是'StyleItem'在'Style'中替换'ComboBoxItem'

0

我只想提到我在两天内遇到类似问题(我的Windows Data Error 4错误,抱怨Horizo​​ntalContentAlignment和VerticalContentAlignment)。最常见的建议解决方案(将Horizo​​ntal/VerticalContentAlignment样式添加到元素,甚至添加到App.xaml)并不总能解决问题。

最终,我发现了一些与我自己的情况相似的东西 - 我希望它可以对某人有所帮助:如果您使用FilterEventHandler,请勿在重新订阅前取消订阅!

我以前的代码保存在生成​​“数据错误4”的消息时,我改变信道滤波器(它调用UpdateCorporatesList):

// This code generates errors 
private void UpdateCorporatesList() 
{ 
    this.CorporatesViewSource.Filter -= new FilterEventHandler(ApplyCorporateFilter); 

    if (this.ChannelFilter != null) 
    { 
     this.CorporatesViewSource.Filter += new FilterEventHandler(ApplyCorporateFilter); 
    } 
    else 
    { 
     this.CorporateFilter = null; 
    } 
} 

private void ApplyCorporateFilter(object sender, FilterEventArgs e) 
{ 
    SalesCorporate customer = e.Item as SalesCorporate; 
    var currentChannel = this.Channels.FirstOrDefault(x => x.ID == this.ChannelFilter).Description; 
    if ((customer.ID != null) && (customer.Channel != currentChannel)) 
    { 
     e.Accepted = false; 
    } 
} 

...所以我改成了重新订阅FilterEventHandler,而是在事件处理方法的Channel Filter中检查一个空值。

// This code works as intended 
private void UpdateCorporatesList() 
{ 
    this.CorporatesViewSource.Filter += new FilterEventHandler(ApplyCorporateFilter); 

    if (this.ChannelFilter == null) 
    { 
     this.CorporateFilter = null; 
    } 
} 

private void ApplyCorporateFilter(object sender, FilterEventArgs e) 
{ 
    var currentChannel = this.Channels.FirstOrDefault(x => x.ID == this.ChannelFilter); 
    if (currentChannel.ID == null) 
    { 
     return; 
    } 

    SalesCorporate customer = e.Item as SalesCorporate; 
    if ((customer.ID != null) && (customer.Channel != currentChannel.Description)) 
    { 
     e.Accepted = false; 
    } 
} 

Et Voila!没有更多的错误:-)