在datatemplate中绑定图像

时间:2016-06-01 19:32:37

标签: wpf mvvm binding datacontext

我正在尝试将一个菜单添加到我的应用程序中,我希望我的菜单标题是当前连接的用户的图片。

我的问题是除了图像之外的每个绑定工作,从我在网上理解的DataTemplate我用来替换图片的菜单标题无法访问它的父Datacontext。我尝试使用一些东西,如相对来源和其他技巧,但没有运气(与我在mvvm的新事实相关,我显然还有很多东西需要学习:))

这是我正在使用的代码:

<Grid DataContext="{Binding User}">
    <DockPanel>
        <Label Content="{Binding Strings.Hello, Source={StaticResource StringLocalizer} }" VerticalAlignment="Center" Padding="0,0,5,0" FontFamily="{DynamicResource Font_Normal}" FontSize="15" Foreground="White"/>
        <TextBlock Text="{Binding DisplayName, TargetNullValue='Guest', FallbackValue='Guest'}" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="{DynamicResource Font_Normal}" FontSize="15" Foreground="White"/>


        <Menu Name="LogonMenu" Margin="10,0,0,0" DataContext="{Binding User}">
            <MenuItem >
                <MenuItem.HeaderTemplate>
                    <DataTemplate>
                        <!--<Image Source="{Binding Datacontext.Image,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Application}}, Converter={StaticResource ImageConverter}, FallbackValue={StaticResource DefaultUserIcon}}"/>-->
                        <Image DataContext="{Binding User}" Source="{Binding Path=Image, Converter={StaticResource ImageConverter}, FallbackValue={StaticResource DefaultUserIcon},Mode=TwoWay}"/>
                    </DataTemplate>
                </MenuItem.HeaderTemplate>

                <MenuItem Header="{Binding Strings.SignIn, Source={StaticResource StringLocalizer}}" Width="150" Margin="10,0,0,0" IsEnabled="{Binding Connected, Converter={StaticResource BoolInverterConverter}}" Visibility="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsEnabled, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.SignInCommand}"/>
                <MenuItem Header="{Binding Strings.SignOut, Source={StaticResource StringLocalizer}}" Margin="10,0,0,0" IsEnabled="{Binding Connected}" Visibility="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsEnabled, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.SignOutCommand}"/>
                <MenuItem Header="{Binding Strings.UserInformation, Source={StaticResource StringLocalizer}}" Margin="10,0,0,0" IsEnabled="{Binding Connected}" Visibility="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsEnabled, Converter={StaticResource BoolToVisibilityConverter}}"/>
            </MenuItem>
        </Menu>
    </DockPanel>
</Grid>

在我的情况下,图像总是显示后备图片,如果有人可以帮我找到一种方法来获取用户的图片,那就太好了。

0 个答案:

没有答案