gpt4 book ai didi

c# - 绑定(bind) UserControl 的数据上下文

转载 作者:行者123 更新时间:2023-12-03 10:50:11 26 4
gpt4 key购买 nike

有点工作,感谢我现在使用的答案:

public class PopupProgramazione : DependencyObject
{
public static readonly DependencyProperty ShowProperty = DependencyProperty.Register("FirstNo", typeof(bool), typeof(PopupProgramazione), null);

public bool Show
{
get { return (bool)GetValue(ShowProperty); }
set { SetValue(ShowProperty, value); }
}
}

在我看来模型:
   public PopupProgramazione Popup
{
get { return _Popup; }
set
{
_Popup = value;
RaisePropertyChanged("Popup");
}
}

public void Programmazione(InterventoSchedeConsuntivi intervento)
{
Popup.Show = true;
InterventoPopupProgramazione = intervento;
}

奇怪的问题来自 xaml :
<local:PopupProgrammazione 
x:Name="popupProg"
Height="300"
Width="400"
Canvas.ZIndex="2"
Canvas.Left="150"
Canvas.Top="150" Grid.RowSpan="4" Grid.Column="2" Margin="7,4,12,296"
Visibility="{Binding Path=Popup.Show, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}}"
DataContext="{Binding Path=InterventoPopupProgramazione}"
>
</local:PopupProgrammazione>

如果我只设置了可见性,它工作得很好,我可以看到我正在使用调试通过转换器。

如果两者都有,则不进行弹出窗口的初始化。(显示弹出窗口,而 Popup.Show=false)。但是如果我关闭弹出窗口:
private void Close_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.Visibility = Visibility.Collapsed;
}

然后通过我的函数打开它,它可以工作但没有通过转换器。

有人可以解释一下那里发生了什么吗?

[编辑]
我没有将我的用户控件专门绑定(bind)到专用对象,而是使用主 xaml 的数据上下文,然后它可以完美运行。
[/编辑]

最佳答案

您需要使用依赖属性

http://msdn.microsoft.com/en-us/library/ms752914.aspx

不错的教程
http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties

基本上你需要设置静态方法......这将注册属性并让你在 XAML 中绑定(bind)到它。然后,您重新指向您的属性以获取和设置在您的代码中使用的依赖属性。

它看起来比实际困难得多;)

编辑:

哦...对不起,也许我误解了..您不能像您为showPopup所做的那样绑定(bind)到 View 模型中的值吗?未能在属性更改事件中注册?

关于c# - 绑定(bind) UserControl 的数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6859269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com