1

There is a post exactly similar to this where the TS binds a Rect instead of Int32Rect to the SourceRect property of CroppedBitmap. My current rep doesn't allow me to comment to ask more question, so I post a new one. Is there any reason why the code below results in an image that is NOT cropped.

ViewModel

public class ViewModel
{
    public Int32Rect SourceRect { set; get; } 
    public ViewModel()
    {
        this.SourceRect = new Int32Rect(5, 5, 5, 5);  
    }
}

XAML

<Grid Name="myGrid">
    <Grid.Resources>
        <BitmapImage x:Key="mainImage" UriSource="someImage.bmp"/>
    </Grid.Resources>
    <StackPanel>
        <Image>
            <Image.Source>
                <CroppedBitmap Source="{StaticResource mainImage}" SourceRect="{Binding SourceRect}" />
            </Image.Source>
        </Image>
    </StackPanel>
</Grid>

XAML.cs

public partial class MainWindow : Window
{
    public MainWindow ()
    {
        InitializeComponent();
        ViewModel viewModel = new ViewModel();
        this.DataContext = viewModel;
    }
}

Update

This explains why this doesn't work as pointed out by NETscape.

Community
  • 1
  • 1
M312V
  • 193
  • 1
  • 14
  • An obvious reason would be that you haven't specified the source object of the `SourceRect` binding. Have you set the `DataContext` of your Window or UserControl to an instance of `ViewModel`? Do you observe any binding error message in Visual Studio's Output Window when running the application in the debugger? – Clemens Nov 14 '13 at 08:24
  • I'm sorry I didn't include that part of code, but yes I've set the DataContext. There are no error messages. I'm using VS10, WPF 4. Does binding to SourceRect work for you? – M312V Nov 14 '13 at 17:41
  • 3
    see [this](http://stackoverflow.com/questions/1028519/trying-to-change-a-croppedbitmaps-sourcerect-at-runtime) answer. – Kcvin Nov 14 '13 at 18:41

0 Answers0