2010-04-19 94 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!沒有更多的錯誤:-)