设置 DataContext 后重置依赖属性
我在 WPF 中有一个 UserControl (AgreementDetails),具有以下 DependencyProperty 和功能:
// UserControl AgreementDetails
public int AgreementID
{
get { return Convert.ToInt32(GetValue(AgreementIDProperty)); }
set { SetValue(AgreementIDProperty, value); }
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
int id = AgreementID;
if (id > 0)
{
GetData();
SetBindingContext();
this.Visibility = System.Windows.Visibility.Visible;
}
else
{
this.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void GetData()
{
ConsultantServiceClient client = new ConsultantServiceClient();
_contract = new UC1001_ActiveAgreementContract();
_contract = client.GetAgreementDetailsByAgreementID(AgreementID);
}
private void SetBindingContext()
{
this.DataContext = _contract;
}
我使用此 UserControl 在另一个 UserControl(仪表板)中显示为工具提示,我在其中设置了greetingID属性:
// Dashboard
<Setter Property="DataGridCell.ToolTip">
<Setter.Value>
<my:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88" AgreementID="{Binding Months[9].AgreementID}"/>
</Setter.Value>
</Setter>
在AgreementDetails中,我使用AgreementID从中获取一些数据要在用户控件中显示的数据库。我第一次这样做,一切都很顺利。但是,当我将传入的WCF DataContract设置为AgreementDetails中的数据上下文时,AgreementID属性重置为0,因此第二次调用将不起作用,因为显然我没有AgreementID = 0的协议。我检查了,AgreementID在SetBindingContext中重置();设置 DataContext 后的方法。
在AgreementDetails中设置新的dataContext后,如何才能使AgreementID属性不会重置?
如果需要,可以提供更多信息。
编辑:我现在有以下代码:
// Dependency properties
public int AgreementID
{
get { return (int)GetValue(AgreementIDProperty); }
set { SetValue(AgreementIDProperty, value); }
}
public UC1001_ActiveAgreementContract AgreementDetailsContract
{
get { return (UC1001_ActiveAgreementContract)GetValue(AgreementDetailsContractProperty); }
set { SetValue(AgreementDetailsContractProperty, value); }
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
public static readonly DependencyProperty AgreementDetailsContractProperty = DependencyProperty.Register("AgreementDetailsContract", typeof(UC1001_ActiveAgreementContract), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
int id = AgreementID;
if (id > 0)
{
GetData();
SetBindingContext();
this.Visibility = System.Windows.Visibility.Visible;
}
else
{
this.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void GetData()
{
ConsultantServiceClient client = new ConsultantServiceClient();
AgreementDetailsContract = client.GetAgreementDetailsByAgreementID(AgreementID);
}
private void SetBindingContext()
{
this.DataContext = AgreementDetailsContract;
}
设置 DataContext 后,AgreementID 重置为 0 的问题仍然存在。
此外,当我使用以下语句进行绑定时,我得到一个空标签:
<Label Content="{Binding RelativeSource={RelativeSource Self}, Path=AgreementDetailsContract.EndClientName}" />
已解决:
我删除了 SetDataBinding() 方法,因此绑定不会重置我的 DependencyProperty,并且对于我的标签的绑定,我使用以下绑定(而不是RelativeSource Self):
<Label Content="{Binding ElementName=AgreementDetails, Path=AgreementDetailsContract.EndClientName}" Grid.Column="1" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="11,0,0,0" Name="_labelEindklant" VerticalAlignment="Top" />
ElementName=AgreementDetails 是我的UserControl 的名称。奇怪的是,{RelativeSource Self} 不起作用......
I have a UserControl (AgreementDetails) in WPF with the following DependencyProperty and function:
// UserControl AgreementDetails
public int AgreementID
{
get { return Convert.ToInt32(GetValue(AgreementIDProperty)); }
set { SetValue(AgreementIDProperty, value); }
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
int id = AgreementID;
if (id > 0)
{
GetData();
SetBindingContext();
this.Visibility = System.Windows.Visibility.Visible;
}
else
{
this.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void GetData()
{
ConsultantServiceClient client = new ConsultantServiceClient();
_contract = new UC1001_ActiveAgreementContract();
_contract = client.GetAgreementDetailsByAgreementID(AgreementID);
}
private void SetBindingContext()
{
this.DataContext = _contract;
}
I use this UserControl to show as a tooltip in another UserControl (Dashboard) where I set the AgreementID property:
// Dashboard
<Setter Property="DataGridCell.ToolTip">
<Setter.Value>
<my:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88" AgreementID="{Binding Months[9].AgreementID}"/>
</Setter.Value>
</Setter>
In AgreementDetails, I use the AgreementID to get some data from the database to show in the UserControl. The first time I do this, everything goes smooth. But when I set the incoming WCF DataContract as the datacontext in AgreementDetails, the AgreementID property resets to 0, so the second call will not work because obviously I do not have an agreement with AgreementID = 0. I checked and the AgreementID resets in the SetBindingContext(); method after the DataContext is set.
How can I make it so the AgreementID property will not reset after I set a new dataContext in AgreementDetails??
More information can be provided if wanted.
EDIT: I now have the following code:
// Dependency properties
public int AgreementID
{
get { return (int)GetValue(AgreementIDProperty); }
set { SetValue(AgreementIDProperty, value); }
}
public UC1001_ActiveAgreementContract AgreementDetailsContract
{
get { return (UC1001_ActiveAgreementContract)GetValue(AgreementDetailsContractProperty); }
set { SetValue(AgreementDetailsContractProperty, value); }
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
public static readonly DependencyProperty AgreementDetailsContractProperty = DependencyProperty.Register("AgreementDetailsContract", typeof(UC1001_ActiveAgreementContract), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
int id = AgreementID;
if (id > 0)
{
GetData();
SetBindingContext();
this.Visibility = System.Windows.Visibility.Visible;
}
else
{
this.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void GetData()
{
ConsultantServiceClient client = new ConsultantServiceClient();
AgreementDetailsContract = client.GetAgreementDetailsByAgreementID(AgreementID);
}
private void SetBindingContext()
{
this.DataContext = AgreementDetailsContract;
}
I still have the the problem that the AgreementID resets to 0 after the DataContext is set.
Also when I use the following statement to bind, I get an empty label:
<Label Content="{Binding RelativeSource={RelativeSource Self}, Path=AgreementDetailsContract.EndClientName}" />
SOLVED:
I removed the SetDataBinding() method so the Binding doesn't reset my DependencyProperty, and for the Binding of my labels I used the following Binding (instead of RelativeSource Self):
<Label Content="{Binding ElementName=AgreementDetails, Path=AgreementDetailsContract.EndClientName}" Grid.Column="1" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="11,0,0,0" Name="_labelEindklant" VerticalAlignment="Top" />
ElementName=AgreementDetails is the name of my UserControl. Strange enough with {RelativeSource Self} it didn't work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在用户控件中设置数据上下文时,您实际上也在重置父控件(仪表板)中的数据上下文。这是相同的上下文。因此,您的协议 ID 不再位于上下文中,因此会被重置。
编辑:其实我说得不太好。您不会影响仪表板中的数据上下文,但会影响该控件中声明的AgreementId 绑定所使用的数据上下文。绑定是在仪表板控件中声明的,但绑定实际上是在您正在重置的子控件的数据上下文中查找。
在这里查看我的类似问题:
在 UserControl 中设置 DataContext 会影响父级中的绑定
<强>编辑:这就是我的意思:
然后在您的AgreementDetails.xaml中,您可能有类似的内容:
哪个绑定需要更改为:
When you set the datacontext in your Usercontrol, you are actually resetting the data context in the parent control too (Dashboard). It's the same context. Because of this your Agreement ID is no longer in the context and so gets reset.
Edit: Actually I didn't word that very well. You're not affecting the data context in Dashboard, but you ARE affecting the data context used by the AgreementId binding declared in that control. The binding is declared in the Dashboard control, but the binding is actually looking in the data context of the child control, which you are resetting.
See my similar question here:
Setting DataContext within UserControl is affecting bindings in parent
EDIT: Here is what I mean:
Then in your AgreementDetails.xaml, you probably have something like:
which binding needs to change to: