簡體   English   中英

動態綁定到資源的“路徑”

[英]Dynamic binding to a “Path” of the resource

首先是我開始的代碼:

<ribbon:RibbonMenuButton IsEnabled="{Binding ForegroundIsConfigurable}"
          SmallImageSource="{Binding Source={StaticResource imageSource},
                             Path=Source,
                             UpdateSourceTrigger=OnPropertyChanged}">

雖然這個綁定正在編譯並運行良好,但我不滿意的原因是imageSource在運行時發生了變化。

StaticResource標記擴展 :通過查找對已定義資源的引用,為任何XAML屬性屬性提供值。 該資源的查找行為類似於加載時查找,它將查找先前從當前XAML頁面的標記以及其他應用程序源加載的資源,並將生成該資源值作為運行中的屬性值時間對象。

由於imageSource值在運行時期間發生變化,我不得不將StaticResource更改為DynamicResource 但屬性Source不是依賴屬性,因此以下代碼將引發運行時錯誤:

SmallImageSource="{Binding Source={DynamicResource imageSource},
                   Path=Source,
                   UpdateSourceTrigger=LostFocus}

出於這個原因,我需要將動態資源直接綁定到SmallImageSource ,這是一個依賴屬性:

SmallImageSource="{DynamicResource imageSource}"

這又會引發運行時錯誤,因為imageSourceImage類型。 SmallImageSource期望該值是ImageSource類型。

現在可以建議將數據上下文設置為我的動態資源並適當地綁定屬性。 如果我這樣做,我會殺死具有另一個DataContext的屬性IsEnabled的綁定。

據我所知, MultiBinding也不是一個解決方案,因為它提供了一種機制,可以將屬性綁定到多個源,但不提供針對不同上下文和源的綁定不同屬性。

在考慮如何繼續下去時,我想到幸運的是,我可以將ImageSource控制器移動到IValueConverter 在我的RibbonMenuButton的給定數據上下文中,我有一個具有適當值的字符串值,它實際上也是我的ImageSource的源。

無論如何,我仍然想知道如果我沒有其他方法,即如果兩個源都在不同的數據上下文中,我將如何解決問題。 有什么我沒看到的嗎? 如何通過覆蓋DataContext並通過綁定動態資源的屬性來確保不殺死其他綁定?


imageSourceDrawingImage msdn頁面上的XAML示例非常相似。

<Image x:Key="imageSource">
  <Image.Source>
    <DrawingImage>
...

您可以嘗試將“imageResource”定義為ImageSource而不是Image 這適合我。

<r:RibbonWindow
    x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    xmlns:pc="clr-namespace:System.Windows.Media;assembly=PresentationCore">
    <Grid>
        <Grid.Resources>
            <pc:ImageSource x:Key="imageSource">your_image.png</pc:ImageSource>
        </Grid.Resources>
        <r:Ribbon>
            <r:RibbonMenuButton
                IsEnabled="{Binding ForegroundIsConfigurable}"
                SmallImageSource="{DynamicResource imageSource}">
            </r:RibbonMenuButton>
        </r:Ribbon>
    </Grid>
</r:RibbonWindow>

此外,您可以通過使用ElementName綁定來設置RibbonMenuButton的DataContext而不覆蓋IsEnabled,如下所示。

<r:RibbonWindow x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="root">

        <Grid.Resources>
            <Image x:Key="imageSource" Source="{Binding myImageSource}"/>
        </Grid.Resources>

        <r:Ribbon>
            <r:RibbonMenuButton DataContext="{DynamicResource imageSource}"
                IsEnabled="{Binding ElementName=Root, Path=DataContext.ForegroundIsConfigurable}"
                SmallImageSource="{Binding Source}"/>
        </r:Ribbon>
    </Grid>
</r:RibbonWindow>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM