我有一个项目控件的样式,它绑定到目标中的图像,它只有在目标也绑定到图像时才会出现,我不知道为什么..任何人都可以为我阐明它吗?
我的风格的简化版本:
<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; }
}
如果我取消注释用户控件中的标记,则图像会同时显示在控件和模板区域中,如果我注释它,它不会出现在任何一个中。模板中的文本绑定工作正常
我很困惑。。
谢谢
特雷弗