1
我有一個項目控件綁定到目標圖像的樣式,它只會出現在目標也綁定到圖像,我不知道爲什麼..任何人都可以爲我闡明任何光?爲什麼我有一個與控件模板中的圖像綁定的問題?
的簡化版本的我的風格:
<Style x:Key="testStyle" TargetType="ItemsControl">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" LastChildFill="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" DockPanel.Dock="Top" MinHeight="25" SnapsToDevicePixels="True">
<StackPanel Orientation="Horizontal">
<Image Margin="10,0,10,0" VerticalAlignment="Stretch" Height="24" Width="24" Source="{Binding Path=HeaderImage}" />
<TextBlock FontFamily="Tahoma" VerticalAlignment="Center" Text="{Binding Path=HeaderInfo}" />
</StackPanel>
<Line VerticalAlignment="Bottom" Stretch="Fill"/>
</Grid>
<ItemsPresenter Grid.Row="1"/>
</Grid>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的用戶控件:
<UserControl x:Class="StartPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<ItemsControl Style="{DynamicResource testStyle}">
<Grid HorizontalAlignment="Stretch" >
<StackPanel>
<GroupBox Header="Information" Margin="0,0,0,10" >
<Label Margin="10,10,10,110">some useful information, dynamically updated</Label>
</GroupBox>
<GroupBox Header="Available actions" Margin="0,10,0,10">
<StackPanel>
<Label Margin="10,10,10,10">action 1</Label>
<Label Margin="10,10,10,10">action 2</Label>
<Label Margin="10,10,10,10">action 3</Label>
<!--<Image Width="0" Height="0" Source="{Binding HeaderImage}"/>-->
</StackPanel>
</GroupBox>
</StackPanel>
</Grid>
</ItemsControl>
我的模型代碼(設置爲數據上下文爲我的用戶控件)
internal class StartPageViewPresentationModel : IStartPageViewPresentationModel
{
public StartPageViewPresentationModel(IStartPageView view)
{
HeaderImage = new BitmapImage(new Uri(@"Images/home_16.png", UriKind.Relative)) { CacheOption = BitmapCacheOption.Default };
HeaderInfo = "Start Page";
View = view;
View.Model = this;
}
public BitmapImage HeaderImage { get; set; }
public string HeaderInfo { get; set; }
public IStartPageView View { get; set; }
}
如果我在用戶控件中取消註釋標記,則圖像顯示在控件和模板區域中,如果我評論它並不出現在其中。該文本從模板結合工作正常
我迷茫..
感謝
特雷弗
太棒了!謝謝! 你是對的,去絕對URI修復它 HeaderImage = new BitmapImage(new Uri(「pack:// application:,,,/Myassembly.UI; component/Images/home_16.png」)); 一旦您指出風格是在一個單獨的組裝中,這是有道理的。 Trev – Trev 2008-11-18 14:40:50