gpt4 book ai didi

wpf - 如何将用户控件的属性绑定(bind)到 WPF 中同一控件的属性?

转载 作者:行者123 更新时间:2023-12-04 20:35:34 24 4
gpt4 key购买 nike

在我的用户控件中,我有一个集合调用

 public List<string> Solutions { get; set; }

我想将该属性绑定(bind)到同一用户控件的 xaml 中的组合框吗?

我试过
<ComboBox HorizontalAlignment="Left" Margin="21,0,0,41" Name="cbAddSolution" Width="194" Height="21" VerticalAlignment="Bottom" 
ItemsSource="{Binding Path=Solutions}" />

但这似乎不起作用。

最佳答案

在 XAML 中命名您的 UserControl 并从绑定(bind)中引用它,如下所示:

<UserControl x:Name = "MyUserControl">
<ComboBox HorizontalAlignment="Left" Margin="21,0,0,41" Name="cbAddSolution" Width="194"
Height="21" VerticalAlignment="Bottom"
ItemsSource="{Binding ElementName=MyUserControl, Path=Solutions}" />
</UserControl>

如果您想要正确绑定(bind)您的 UserControl 应该为该属性实现 INotifyPropertyChanged 或使该属性成为依赖属性

更新

或者如果您不想命名 UserControl,请使用 RelativeSource
<UserControl>
<ComboBox HorizontalAlignment="Left" Margin="21,0,0,41" Name="cbAddSolution" Width="194"
Height="21" VerticalAlignment="Bottom"
ItemsSource="{Binding Path=Solutions, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</UserControl>

关于wpf - 如何将用户控件的属性绑定(bind)到 WPF 中同一控件的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1933650/

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