gpt4 book ai didi

c# - Wpf 绑定(bind)桥

转载 作者:行者123 更新时间:2023-11-30 14:55:34 28 4
gpt4 key购买 nike

我感觉这是 wpf 中的一个错误。让我知道你们对此有何看法。

为了简单起见,我在 .net 4.0 中制作了演示示例

我有一个 ContentControl,其中 Content 绑定(bind)到 Data 和 ContentTemplate,其中包含一个 CheckBox 绑定(bind)到 Content。

问题是无论我点击 CheckBox 多少次,Ok 属性永远不会为真。

好像 CheckBox 没有将新值传递给 ViewModel。

看看这个:

  <Window.Resources>
<DataTemplate x:Key="dataTemplate">
<CheckBox IsChecked="{Binding Path=., Mode=TwoWay}"/>
</DataTemplate>
</Window.Resources>

<Grid>
<ContentControl Content="{Binding Path=Ok, Mode=TwoWay}" ContentTemplate="{StaticResource dataTemplate}"/>
</Grid>

这是我的 View 模型

public MainWindow()
{
InitializeComponent();

ViewModel vm = new ViewModel();
this.DataContext = vm;
}

public class ViewModel : INotifyPropertyChanged
{
private string txt;

public string Txt
{
get { return txt; }
set { txt = value; this.OnPropertyChanged("Txt"); }
}

private bool ok;

public bool Ok
{
get { return ok; }
set { ok = value; this.OnPropertyChanged("Ok");}
}


private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

关于如何使用 ContentTemplate 解决这个问题有什么想法吗?

最佳答案

您的问题是与使用值类型相关的常见问题。您将复选框数据绑定(bind)到原始值类型 (bool)(这是一件非常不常见的事情)。因为 Binding.Sourceobject 类型,所以您的 bool 值被装箱到一个 object 中。该装箱对象的任何更新都不会影响 ViewModel 上的原始属性。

您可以通过用这样的结构替换该 bool 值来测试这个理论:

public struct MyStruct
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; }
}

public ViewModel Parent { get; set; }
}

并更改您的绑定(bind):

<CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>

如果您在 IsChecked getter 和 setter 上放置断点,您将看到绑定(bind)有效。但是,当您遇到其中一个断点时,请尝试在立即窗口中调查此值:

? this.Parent.Ok.IsChecked

您应该看到父 View 模型上的 MyStruct 属性根本没有受到数据绑定(bind)的影响。


完整测试代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

ViewModel vm = new ViewModel();
vm.Ok = new MyStruct { Parent = vm, IsChecked = false };
this.DataContext = vm;
}


}

public class ViewModel : INotifyPropertyChanged
{
private string txt;

public string Txt
{
get { return txt; }
set { txt = value; this.OnPropertyChanged("Txt"); }
}

private MyStruct ok;

public MyStruct Ok
{
get { return ok; }
set { ok = value; this.OnPropertyChanged("Ok"); }
}


private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

public struct MyStruct
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; }
}

public ViewModel Parent { get; set; }
}
}

xaml:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="dataTemplate">
<CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
</DataTemplate>
</Window.Resources>

<Grid>
<ContentControl Content="{Binding Path=Ok, Mode=TwoWay}" ContentTemplate="{StaticResource dataTemplate}"/>
</Grid>
</Window>

关于c# - Wpf 绑定(bind)桥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134451/

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