Can you bind to the ImageSource property of an ImageBrush with Caliburn.Micro (WinRT)?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 04:16:32

问题


I was hoping to use dynamic background images with Caliburn.Micro. This is what I have tried without success.

<Grid>
 <Grid.Background>
   <ImageBrush x:Name="MyPhoto" /> 
 </Grid.Background>
</Grid>

//some view model
public class ImageViewModel
{
   public ImageSource MyPhoto {get;set;}
}

//Add Convention
//App.XAML.cs
...
public override void Configure()
{
  ...

        ConventionManager.AddElementConvention<ImageBrush>(ImageBrush.ImageSourceProperty, "ImageSource", "Loaded");
  ...
}

Is it possible to bind and ImageBrush's ImageSource with Caliburn.Micro or is there a better way to do this?


回答1:


Not sure but I think AddElementConvention works only for UIElements not DependencyObjects. This should work, though:

MainPage.xaml:

<Grid x:Name="MyBrush">
</Grid>

MainPageViewModel.cs:

public class MainPageViewModel : Screen
{
    public MainPageViewModel()
    {
        MyPhoto = new BitmapImage(new Uri("ms-appx:///Assets/SplashScreen.png"));
    }

    public ImageSource MyPhoto { get; set; }

    public ImageBrush MyBrush
    {
        get
        {
            ImageBrush brush = new ImageBrush();
            brush.ImageSource = MyPhoto;
            return brush;
        }
    }
}

App.xaml.cs:

protected override void Configure()
{
    container = new WinRTContainer();
    container.RegisterWinRTServices();

    ConventionManager.AddElementConvention<Grid>(Grid.BackgroundProperty, "Background", "Loaded");
}

Alternatively you could do the binding by hand in XAML:

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="{Binding MyPhoto}" /> 
    </Grid.Background>
</Grid>


来源:https://stackoverflow.com/questions/13756909/can-you-bind-to-the-imagesource-property-of-an-imagebrush-with-caliburn-micro-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!