2011-11-23 54 views
2

我們有一些XAML可能看起來很奇怪,但顯然需要在第三方的Ribbon Gallery控件中定義幾個按鈕。該庫有一個ItemsControl.ItemsSource,其中XAML正在填充兩個數組項,這些數組項是具有位圖屬性和ICommand屬性的自定義類型。一切看起來不錯,但我無法獲得數組項屬性綁定到任何與窗口的數據上下文。我已經嘗試了我所知道的每一個技巧,RelativeSource,ElementName但無濟於事。下面是XAML:綁定到DataContext屬性內聯行數組聲明

<ribbon:RibbonGallery.ItemsSource> 
    <x:Array Type="{x:Type customUITypes:ClickableImage}"> 
     <customUITypes:ClickableImage x:Name="BitmapAddWorkflow" Command="{Binding ElementName=MainView, Path=DataContext.MyCommandOne}"> 
      <customUITypes:ClickableImage.Bitmap> 
       <BitmapImage UriSource="/Images/GalleryWorkflowAdd.png"/> 
      </customUITypes:ClickableImage.Bitmap> 
     </customUITypes:ClickableImage> 
     <customUITypes:ClickableImage x:Name="BitmapDeleteWorkflow" Command="{Binding ElementName=MainView, Path=DataContext.MyCommandTwo}"> 
      <customUITypes:ClickableImage.Bitmap> 
       <BitmapImage UriSource="/Images/GalleryWorkflowDelete.png"/> 
      </customUITypes:ClickableImage.Bitmap> 
     </customUITypes:ClickableImage>           
    </x:Array> 
</ribbon:RibbonGallery.ItemsSource> 
<ribbon:RibbonGallery.ItemTemplate> 
    <DataTemplate> 
     <Button Command="{Binding Command}"> 
      <Image Margin="2" Source="{Binding Bitmap}" Stretch="None"/> 
     </Button> 
    </DataTemplate> 
</ribbon:RibbonGallery.ItemTemplate> 

注:的MainView是窗口的名稱,該數據上下文爲100%,我想要的,我有這種觀點的任何其他綁定沒有問題,只是在這個數組定義。

我想我對於我可以訪問的對象的層次結構感到困惑,但在我看來,無論我是否在數組定義標記內進行綁定,我仍然應該能夠找到一個元素並綁定到它的數據上下文。有時候,XAML似乎有不一致的問題,它會造成數小時的頭部劃傷,只是爲了簡單。我意識到我可以在我的視圖模型中對一些這樣的代碼進行硬編碼,即在代碼中創建我的數組項並綁定到該項,但我想避免這樣做,因爲這意味着在代碼中硬編碼圖像路徑,我感覺圖像路徑是標記聲明。

任何幫助將不勝感激。

感謝

保羅

回答

3

你有沒有使用「溝ElementName和使用Source & x:Reference招了嗎?

<Window.Resources> 
    <x:Array x:Key="Items" Type="{x:Type customUITypes:ClickableImage}"> 
     <customUITypes:ClickableImage x:Name="BitmapAddWorkflow" 
       Command="{Binding DataContext.MyCommandOne, Source={x:Reference MainWindow}}"> 
      <customUITypes:ClickableImage.Bitmap> 
       <BitmapImage UriSource="/Images/GalleryWorkflowAdd.png"/> 
      </customUITypes:ClickableImage.Bitmap> 
     </customUITypes:ClickableImage> 
     <customUITypes:ClickableImage x:Name="BitmapDeleteWorkflow" 
       Command="{Binding DataContext.MyCommandTwo, Source={x:Reference MainWindow}}"> 
      <customUITypes:ClickableImage.Bitmap> 
       <BitmapImage UriSource="/Images/GalleryWorkflowDelete.png"/> 
      </customUITypes:ClickableImage.Bitmap> 
     </customUITypes:ClickableImage>           
    </x:Array> 
</Window.Resources> 
<!-- ... --> 
<ribbon:RibbonGallery ItemsSource="{StaticResource Items}" ... 

由於週期性依賴(你可以嘗試保持原位,但我敢肯定編譯器不會喜歡它)。


或者,你可以從一個管道對象搶奪的DataContext:

<Window.Resources> 
    <!-- Resource declaration gives you easy access using StaticResource --> 
    <FrameworkElement Name="Pipe" Visibility="Hidden"/> 
</Window.Resources> 
<!-- Place it somewhere in the window where it can inherit the Window's DataContext --> 
<StaticResource ResourceName="Pipe"/> 
<!-- ... --> 
<customUITypes:ClickableImage x:Name="BitmapAddWorkflow" 
    Command="{Binding DataContext.MyCommandOne, Source={StaticResource Pipe}}"> 
+0

工程就像一個魅力,這就是爲什麼我喜歡堆棧溢出。非常感謝。出於好奇,在我的答案中,我是否感到一絲諷刺?我並不是說我的問題聽起來很自負,我很沮喪,我無法弄清楚,你的答案最後很簡單但很有效,我意識到我仍然有很多東西要學習WPF。 –

+0

此外,感謝您在我的問題中格式化XAML,我想它原本是複製粘貼失敗:)。謝謝 –

+0

@PaulieWaulie:這並不意味着諷刺,我只是意識到WPF中有很多「技巧」,我懷疑任何人都會了解它們。很高興工作:) –