【发布时间】:2021-10-22 13:51:30
【问题描述】:
我最近开始使用 WPF,所以我并不了解所有内容。 我的 Xaml 文件中有一个列表视图,其中列出了某个类的对象。
我创建了一个自定义用户控件(切换按钮)来切换对象的布尔属性。 以前,我使用复选框,一切正常。
现在绑定总是返回 False。
我不知道如何让它工作
我的班级:
public class Macro : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public const int EDITPIECE = 1, WALLREDITPIECE = 2, DORMIR = 3, NO_ACTION = 0;
public static double speed = 0;
private bool _status;
public bool status {
get {
return _status;
}
set {
if (value != this._status)
{
this._status = value;
NotifyPropertyChanged();
}
}
}
private string _key;
public string key {
get
{
return _key;
}
set
{
if (value != this._key)
{
this._key = value;
NotifyPropertyChanged();
}
}
}
public string _macroname { get; set; }
public int _action { get; set; }
...
}
我的列表视图:
<ListView Margin="10" Name="lvDataBinding" Width="auto" Background="Transparent" Foreground="white" BorderThickness="0">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding _macroname}" FontWeight="Bold" />
<TextBlock Text=" " />
<!-- Checkbox works fine --> <CheckBox IsChecked="{Binding status , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Checked="CheckBoxChanged" Unchecked="CheckBoxChanged"/>
<!-- Custom Toggle not working --> <theme:OnOff Height="24" Width="40" Toggle="{Binding status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text=" key : " />
<Button x:Name="keybutton" Content="{Binding key, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Click="keybutton_Click"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
自定义用户控件:
public static readonly DependencyProperty ToggleProperty = DependencyProperty.Register("Toggle", typeof(bool), typeof(OnOff), new PropertyMetadata(false));
public bool Toggle
{
get { return (bool)base.GetValue(OnOff.ToggleProperty); }
set { base.SetValue(OnOff.ToggleProperty, value); }
}
如果填满了我的清单:
public partial class Page1 : Page
{
private List<Macro> listMacroConfig = new List<Macro>();
public Page1()
{
InitializeComponent();
listMacroConfig = ((App)Application.Current)._macros;
lvDataBinding.ItemsSource = listMacroConfig;
}
【问题讨论】:
-
您是否明确设置了 UserControl 的 DataContext,例如在其 XAML 或代码后面?那将是问题所在。您如何注意到绑定不起作用?没有 PropertyChangedCallback,属性的可视化可能不起作用(你没有向我们展示那部分)。
-
请注意,
GetValue(ToggleProperty)等同于base.GetValue(OnOff.ToggleProperty)。 -
您的 OnOff 类是否继承自 WPF
ToggleButton类?如果没有,您将需要实现让鼠标和键盘输入自行更改ToggleProperty值的代码。您的依赖属性将绑定到视图模型属性,但它们都不会因为它们存在而在用户交互时改变。您需要在控件模板中连接功能... -
此外,即使
OnOff将继承自ToggleButton,点击其 UI 表面仍不会转换为对Toggle设置器的调用。点击仍然只会切换IsChecked属性。
标签: c# wpf xaml user-controls dependency-properties