GVKun编程网logo

如何将 WPF 绑定与 RelativeSource 结合使用?(wpf 绑定 selecteditems)

14

想了解如何将WPF绑定与RelativeSource结合使用?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于wpf绑定selecteditems的相关问题,此外,我们还将为您介绍关于.ne

想了解如何将 WPF 绑定与 RelativeSource 结合使用?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于wpf 绑定 selecteditems的相关问题,此外,我们还将为您介绍关于.net – Silverlight 4 RelativeSource FindAncestor binding [closed]、c# – (wpf)Application.Current.Resources vs FindResource、c# – WPF BitmapSource ImageSource、c# – 使用MVVM的WPF:使用RelativeSource的DataBinding的新知识。

本文目录一览:

如何将 WPF 绑定与 RelativeSource 结合使用?(wpf 绑定 selecteditems)

如何将 WPF 绑定与 RelativeSource 结合使用?(wpf 绑定 selecteditems)

如何使用RelativeSourceWPF 绑定以及有哪些不同的用例?

答案1

小编典典

如果要绑定到对象上的另一个属性:

{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}

如果您想获得祖先的属性:

{Binding Path=PathToProperty,    RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

如果您想在模板化父对象上获取属性(因此您可以在 ControlTemplate 中进行 2 路绑定)

{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}

或者,更短(这只适用于 OneWay 绑定):

{TemplateBinding Path=PathToProperty}

.net – Silverlight 4 RelativeSource FindAncestor binding [closed]

.net – Silverlight 4 RelativeSource FindAncestor binding [closed]

在Silverlight 4中会有RelativeSource FindAncestor,AncestorType …吗?

解决方法

在Silverlight 4中,Binding的RelativeSource属性仍然只支持“Self”和“TemplatedParent”,在此区域中,Silverlight 3没有任何变化。

c# – (wpf)Application.Current.Resources vs FindResource

c# – (wpf)Application.Current.Resources vs FindResource

所以,我正在使用C#中的 WPF创建一个GUI.它看起来像这样:

它现在还没有完成.这两行是我尝试制作一种数据表,它们在XAML中是硬编码的.

现在,我正在C#中实现添加新的水果按钮功能.
我在XAML中有以下样式来控制行的背景图像应该是什么样子:

<Style x:Key="stretchImage" targettype="{x:Type Image}">
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Stretch" Value="Fill"/>
</Style>

因此,在代码中,我为每列col0,col1和col2创建一个图像,如果我使用以下代码,

col0.Style = (Style)Application.Current.Resources["stretchImage"];
col1.Style = (Style)Application.Current.Resources["stretchImage"];
col2.Style = (Style)Application.Current.Resources["stretchImage"];

它添加了一个如下所示的新行:

如你所见,它不太正确……
拉伸窗口会加剧问题:

它似乎不尊重风格的“Stretch”属性.

但是,如果我改为将样式加载代码改为

col0.Style = (Style)FindResource("stretchImage");
col1.Style = (Style)FindResource("stretchImage");
col2.Style = (Style)FindResource("stretchImage");

它工作得很漂亮:

(同样,应用程序还没有完成,所以不要担心),但我的主要问题是:Application.Current.Resources []和FindResource()之间有什么区别?为什么一个似乎忽略了一些属性而另一个没有?如果可能的话,我可以如何使Application.Current.Resources []正常工作?

解决方法

可以在可视化树中的几乎任何元素上定义资源. FrameworkElement.FindResource()将在树上向上查找每个节点上的资源,并最终一直到应用程序. Application.Current.Resources []跳过所有这些并直接获取Application上的资源.您几乎总是希望使用FindResource(),因此您可以在各个点覆盖样式.

c# – WPF BitmapSource ImageSource

c# – WPF BitmapSource ImageSource

我将 Image.source属性绑定到下面显示的属性的结果.
public BitmapSource MyImageSource
{
    get
    {
        BitmapSource source = null;

        PngBitmapDecoder decoder;
        using (var stream = new FileStream(@"C:\Temp\logo.png",FileMode.Open,FileAccess.Read,FileShare.Read))
        {
            decoder = new PngBitmapDecoder(stream,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.None);

            if (decoder.Frames != null && decoder.Frames.Count > 0)
                source = decoder.Frames[0];
        }

        return source;
    }
}

由于某种原因,在渲染图像期间(在PresentationCore程序集中为Deep)失败.我确信图像没有损坏,因为我可以成功显示没有绑定的相同图像

<Image Name="FooImage" Source="/logo.png" />

我必须在代码中绑定图像源,因为我最终将从base64字符串创建图像流.

任何人都知道这是否是WPF的错误?或者我做错了什么?

解决方法

问题是BitmapCacheOption选项,更改为BitmapCacheOption.OnLoad工作.

使用BitmapCacheOption.None,BitmapSource在渲染图像之前不会被解码,但是其中包含png的流已经被放置在那个点上.如果你缓存OnLoad,它会立即解码并缓存结果,而不是在流不再存在时尝试解码.

c# – 使用MVVM的WPF:使用RelativeSource的DataBinding

c# – 使用MVVM的WPF:使用RelativeSource的DataBinding

我有一个控件,在该控件内,我有一个带有数据tempalte的资源:
<DataTemplate DataType="{x:Type local:FlowModel}">
    <Image Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type vm:Mainviewmodel}},Path=Mainviewmodel.ImagePath}"/>
  </DataTemplate>

 xmlns:vm="clr-namespace:CortexMonitoringTool.viewmodel"

我将vm设置为我的viewmodel文件夹,我正在实现mvvm.我不能让我的约束力工作,我不确定为什么不.

有人可以告诉我,如果我的相对绑定是正确的,它是否可以在我的Mainviewmodel类中实际看到我的属性’ImagePath’?

public String ImagePath
    {
        get
        {
            return _imagePath;
        }
        set
        {
            if (_imagePath == value)
            {
                return;
            }
            _imagePath = value;
            RaisePropertyChanged("ImagePath");
        }
    }

谢谢.

解决方法

您查看模型不是Visual树的一部分.所以找到祖先类型不会在那里工作.如果您找到具有datacontext的根父级,则可以使用其属性与like绑定.
<Image Source={...... Path=DataContext.MyProperty}"/>

今天关于如何将 WPF 绑定与 RelativeSource 结合使用?wpf 绑定 selecteditems的分享就到这里,希望大家有所收获,若想了解更多关于.net – Silverlight 4 RelativeSource FindAncestor binding [closed]、c# – (wpf)Application.Current.Resources vs FindResource、c# – WPF BitmapSource ImageSource、c# – 使用MVVM的WPF:使用RelativeSource的DataBinding等相关知识,可以在本站进行查询。

本文标签: