小编tab*_*ina的帖子

通过绑定启用TabItem

我想在不同页面是TabItems的应用程序中使用MVVM.

为此,我使用了一个可观察的视图模型(Items)集合,并将其绑定到tabcontrols ItemSource.

对于每个视图模型,我创建了一个单独的数据模板来呈现正确的视图,如下所示:

<DataTemplate DataType="{x:Type baseVm:AViewModel}">
  <baseVw:AView /> 
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

要在选项卡的标题中显示正确的名称,我创建了另一个数据模板,以应用于每个选项卡控件的元素:

<DataTemplate x:Key="ViewModelTabTemplate">
  <DockPanel>
    <ContentPresenter Content="{Binding Path=Name}"/>
  </DockPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

选项卡控件如下所示:

<TabControl x:Name="myTabControl" 
            ItemsSource="{Binding Items}" 
            ItemTemplate="{DynamicResource ViewModelTabTemplate}">
</TabControl>
Run Code Online (Sandbox Code Playgroud)

我现在要做的是在包含集合的视图模型中启用/禁用选项卡.视图模型的基类包含一个依赖项属性IsEnabled,我想将它绑定到视图.如果我直接在视图中这样做:

IsEnabled="{Binding IsEnabled, FallbackValue=true}"
Run Code Online (Sandbox Code Playgroud)

当我将IsEnabled属性设置为false时,标签页的内容被禁用.但我真正想要的是也禁用tabpage的标签而不仅仅是内容.

谢谢你的帮助!

wpf binding tabcontrol datatemplate mvvm

10
推荐指数
1
解决办法
4747
查看次数

WPF Scrollviewer 滚动到鼠标位置

我正在研究一个图像查看器控件,该控件由滚动查看器内的图像组成。为了缩放,我在图像的 layouttransform 中使用了 scaletransform。Zoomin 在鼠标点击时发生。如果用户单击图像,我希望鼠标位置的像素位于滚动查看器可视区域的中心。为了实现这一点,我想修改滚动查看器的偏移量,但我不确定如何计算将像素置于鼠标指针下方的正确偏移量。

这里描述了如何将滚动条保持在它们的相对位置。这工作正常,但不是我正在寻找的。

你如何正确计算偏移量?

这是我的一些代码:

xml:

<ScrollViewer Margin="5" Name="scrvImagePanel" VerticalAlignment="Stretch" 
                  CanContentScroll="True"
                  DockPanel.Dock="Left" 
                  HorizontalAlignment="Stretch" 
                  HorizontalContentAlignment="Stretch"
                  HorizontalScrollBarVisibility="Auto" 
                  VerticalContentAlignment="Stretch" 
                  VerticalScrollBarVisibility="Auto"
                  Background="{StaticResource {x:Static SystemColors.ControlDarkBrushKey}}"
                  ScrollChanged="OnScrollChanged">
        <Image Name="imgPicture"
               MouseUp="imgMouseUp" 
               Stretch="None"
               HorizontalAlignment="Left" 
               VerticalAlignment="Top" 
               >
            <Image.LayoutTransform>
                <TransformGroup>
                <ScaleTransform x:Name="imgZoomTransform" ScaleX="1" ScaleY="1"></ScaleTransform>
                </TransformGroup>
            </Image.LayoutTransform>                
        </Image>
    </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

和代码:

private void imgMouseUp(object sender, MouseButtonEventArgs e)
{
   Point absScreenPos = e.GetPosition(imgPicture);

    //calculate the new zoom factor
    this.ZoomFactor = imgZoomTransform.ScaleX;
    if (e.ChangedButton == MouseButton.Right)
    {
      this.ZoomFactor /= 2.0;
    }
    else if (e.ChangedButton == MouseButton.Left)
    {
      this.ZoomFactor *= 2.0; …
Run Code Online (Sandbox Code Playgroud)

wpf image scrollviewer offset

6
推荐指数
1
解决办法
5273
查看次数

从字节数组创建BitmapImage

我正在创建一个包含任意值的字节数组,并希望将其转换为BitmapImage.

    bi = new BitmapImage();
    using (MemoryStream stream = new MemoryStream(data))
    {
      try
      {
        bi.BeginInit();
        bi.CacheOption = BitmapCacheOption.OnLoad;
        bi.StreamSource = stream;
        bi.DecodePixelWidth = width;

        bi.EndInit();

      }
      catch (Exception ex)
      {
        return null;
      }
    }
Run Code Online (Sandbox Code Playgroud)

这段代码一直给我一个NotSupportedException.我怎么能从任何字节数组创建一个BitmapSource?

wpf bitmapimage notsupportedexception

5
推荐指数
1
解决办法
2万
查看次数

图像不会在自定义图片框中刷新

我的自定义图片框包含滚动查看器和图像.依赖属性string类型的图像用于设置图像.

public static DependencyProperty ImageProperty = DependencyProperty.Register(
"Image", typeof(string), typeof(CustomPictureBox), new FrameworkPropertyMetadata("", new  PropertyChangedCallback(OnImageChanged)));


private static void OnImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  CustomPictureBox cpb = (CustomPictureBox)d;
  if (e.Property == ImageProperty)
  {
    string newvalue = e.NewValue as string;
    if (!(string.IsNullOrEmpty(newvalue)))
    {
      var bmp = new BitmapImage();
      bmp.BeginInit();
      bmp.UriSource = new Uri(newvalue);
      bmp.CacheOption = BitmapCacheOption.OnLoad;
      bmp.EndInit();

      cpb.imgPicture.Source = bmp;
    }
    else
      cpb.imgPicture.Source = null;
  }
}
Run Code Online (Sandbox Code Playgroud)

通过帧抓取器获取图像并将其存储到名为"camera_image.tif"的给定位置.Image属性设置为此文件名.当我开始新的图像采集时,我通过绑定将Image属性设置为null,图片框更新以显示没有图像.完成图像采集后,我再次将其设置为"camera_image.tif".问题是新图像永远不会出现.相反,它始终是图片框中显示的第一个获取的图像.当我检查图像文件时,它包含新内容.

我怎么能让图片框刷新图像?

问候,

tabina

wpf binding image bitmap

4
推荐指数
1
解决办法
5164
查看次数