【发布时间】:2017-06-24 01:57:26
【问题描述】:
如何将设计器中DataContext 的属性更改传播到实际的数据上下文对象?这可能吗?
这是我尝试过的 - 我将 DataContext 转换为 XML 中的本地值 - 我认为我在设计器中对其所做的任何更改都会反映在 DataContext 对象上。
这是 SSCCE。我有一个名为 MammalUC 的 UserControl 和一个名为 Kangaroo 的类。我使用 Kangaroo 类的对象作为 DataContext。下面的代码说明了这一点。
using System.ComponentModel;
using System.Windows.Controls;
namespace WPFTestABC
{
/// <summary>
/// User Control : Interaction logic for MammalUC.xaml
/// </summary>
public partial class MammalUC : UserControl
{
public MammalUC()
{
InitializeComponent();
Kang = new Kangaroo();
this.DataContext = Kang;
}
private Kangaroo kang;
/// <summary>
/// This is the datacontext.
/// </summary>
[Category("ForDebug")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public Kangaroo Kang
{
get{ return kang;}
set {kang = value;}
}
}
/// <summary>
/// Kangaroo class.
/// </summary>
public class Kangaroo : INotifyPropertyChanged
{
private int age;
public int Age
{
get { return age; }
set
{
age = value;
OnPropertyChanged("Age");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
我像这样将 Age 属性绑定到 UserControl -
<UserControl x:Class="WPFTestABC.MammalUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFTestABC"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Text="{Binding Age}" Background="#FFD88787"></TextBox>
</Grid>
</UserControl>
然后我将 MammalUC 放在窗户上。然后将 Kang 对象转换为本地值(我也尝试过作为静态资源)。在设计器属性网格中,我更改了值,但我没有看到正在更新的值。
<Window x:Class="WPFTestABC.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTestABC"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<!--<Window.Resources>
<local:Kangaroo x:Key="Kangaroo1" Age="65"/>
</Window.Resources>-->
<Grid>
<!--Using Static Resource -->
<local:MammalUC HorizontalAlignment="Left" Height="100" Margin="210,115,0,0" VerticalAlignment="Top" Width="100">
<!--Converting to local resource-->
<local:MammalUC.Kang>
<local:Kangaroo Age="85"/> <!--Value never updates-->
</local:MammalUC.Kang>
</local:MammalUC>
</Grid>
</Window>
【问题讨论】:
-
如何将给定对象的属性更改传播到对象本身?这是没有意义的。它是它自己。
-
@Ed - 也许我应该更好地表达这个问题。为什么更改未注册? UI 没有更新。
-
我知道我几天前回答了你的一个问题,你没有费心接受它是正确的,尽管你似乎认为它已经解决了你的问题。如果答案解决了您遇到的问题,您应该接受它。
-
@Ed - 很抱歉。我错过了。将您的解决方案标记为答案。会做得更好。
-
@SanMor,DataContext 对象的附加属性有什么好处?
<local:MammalUC.DataContext><local:Kangaroo Age="85"/></local:MammalUC.DataContext>。 DataContext 可以包含任何对象并通过绑定直接访问
标签: wpf datacontext