wpf属性更新后通知的问题

swtee 2019-01-11 05:29:50
问题:两个checkbox更新后,绑定的属性并没有更新,请教各位大神带这么解决?只有200分,如果哪位能解决,全部奉上
代码如下:
view(XAML文件):
<UserControl.Resources>
<ResourceDictionary>
<local:ShowConverter x:Key="sc"/>
</ResourceDictionary>
</UserControl.Resources>
<CheckBox Content="{Binding BSM.Commander_BTNMain.Name}" IsChecked="{Binding BSM.Commander_BTNMain.IsSelected}" >
<CheckBox Content="{Binding BSM.Commander_BTNSub.Name}" Grid.Column="1" IsChecked="{Binding BSM.Commander_BTNSub.IsSelected}" />
<Label>
<Label.Content>
<Label Grid.Column="1">
<Label.Content>
<MultiBinding Converter="{StaticResource sc}">
<Binding Path="BSM.Commander_BTNMain" Mode="OneWay"/>
<Binding Path="BSM.Commander_BTNSub"/>
</MultiBinding>
</Label.Content>
</Label>
Model:
public class ICheckItem : BindableBase
{
/// <summary>
///当前项是否选中
///</summary>
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
SetProperty(ref isSelected, value);
}
} /// <summary>
///名称
///</summary>
public string Name { get; set; }
}

public class BTNSysDeployModel : BindableBase
{ //主车
private ICheckItem commander_BTNMain = new ICheckItem();

public ICheckItem Commander_BTNMain
{
get { return commander_BTNMain; }
set
{
SetProperty(ref commander_BTNMain, value);
}
}
//副车
private ICheckItem commander_BTNSub = new ICheckItem();

public ICheckItem Commander_BTNSub
{
get { return commander_BTNSub; }
set
{
SetProperty(ref commander_BTNSub, value);
}
}
}
转换器:
public class ShowConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string show = "";
foreach (var item in values)
{
if (item is ICheckItem)
{
ICheckItem ic = (ICheckItem)item;
if (ic.IsSelected)
{
show += ic.Name + " ";
}
}
}

return show;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
...全文
836 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
星辰落 2019-01-28
  • 打赏
  • 举报
回复
INotifyPropertyChanged,还有前端绑定里设置一下mode=twoway
qq14923349 2019-01-23
  • 打赏
  • 举报
回复
BindableBase 这玩意我用过 你要确定 是否特定场景才没收到通知?这是个坑
改用INotify就ok了
swtee 2019-01-20
  • 打赏
  • 举报
回复
自顶,求大神
swtee 2019-01-20
  • 打赏
  • 举报
回复
引用 1 楼 ilikeff8 的回复:
用INotifyPropertyChanged接口更方便简单


<CheckBox Content="上传失败" IsChecked="{Binding SearchInterfaceLogIsNotSuccess, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource AlignmentCheckBoxStyle}" Canvas.Left="664" Canvas.Top="25" Width="92" Height="18"/>



int searchInterfaceLogIsNotSuccess = 0;
public int SearchInterfaceLogIsNotSuccess
{
get
{ return searchInterfaceLogIsNotSuccess; }
set
{
if (searchInterfaceLogIsNotSuccess != value)
{
searchInterfaceLogIsNotSuccess = value;
OnPropertyChanged(nameof(SearchInterfaceLogIsNotSuccess));
}
}
}

public class MainViewModel : INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

1.BindableBase和INotifyPropertyChanged是用是一样的,项目组要求使用前者;
2.情况不一样,您绑定的是一个int类型的属性,我的控件绑定的是一个引用类型的属性
我的问题简单的可以表达为如:
<Label Grid.Row="1" Name="linkLable" Content="{Binding Stu,Converter={StaticResource sc}}" HorizontalAlignment="Center" VerticalAlignment="Center"/ >
private Student _stu = new Student();

public Student Stu
{
get { return _stu; }
set { SetProperty(ref _stu, value); }
}

public class Student : BindableBase
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
SetProperty(ref _name, value);
}
}
int _age;
public int Age
{
get
{
return _age;
}
set
{
SetProperty(ref _age, value);
}
}
}
ilikeff8 2019-01-11
  • 打赏
  • 举报
回复
我程序里返回要用整形,所以定义为int,会自动转换,searchInterfaceLogIsNotSuccess定义成bool也可以
ilikeff8 2019-01-11
  • 打赏
  • 举报
回复
IsChecked返回是整型
ilikeff8 2019-01-11
  • 打赏
  • 举报
回复
用INotifyPropertyChanged接口更方便简单


<CheckBox Content="上传失败" IsChecked="{Binding SearchInterfaceLogIsNotSuccess, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource AlignmentCheckBoxStyle}" Canvas.Left="664" Canvas.Top="25" Width="92" Height="18"/>



int searchInterfaceLogIsNotSuccess = 0;
public int SearchInterfaceLogIsNotSuccess
{
get
{ return searchInterfaceLogIsNotSuccess; }
set
{
if (searchInterfaceLogIsNotSuccess != value)
{
searchInterfaceLogIsNotSuccess = value;
OnPropertyChanged(nameof(SearchInterfaceLogIsNotSuccess));
}
}
}

public class MainViewModel : INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}



8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧