Resharper说OnPropertyChange集成员可以是私有的而不是真的

Mic*_*ers 3 c# wpf resharper set mvvm

我使用C#,MVVM,WPF和Resharper.

使用以下代码时:

    public bool CombiBanksSelected
    {
        get { return _selectedBanksType == ESelectedBanksType.CombiBanks; }
        set
        {
Run Code Online (Sandbox Code Playgroud)

我得到了Resharper的警告:将set accessor设为私有.

将set方法设为私有时,我得到一个InvalidOperationException:TwoWay或OneWayToSource绑定不能对'PcgTools.ViewModels.PcgViewModel'类型的只读属性''CombiBanksSelected''起作用.

当然我可以通过添加以下内容来抑制它:

    public bool CombiBanksSelected
    {
        get { return _selectedBanksType == ESelectedBanksType.CombiBanks; }
// ReSharper disable MemberCanBePrivate.Global
        set
// ReSharper restore MemberCanBePrivate.Global
        {
Run Code Online (Sandbox Code Playgroud)

但这并不好看而且感觉不好.这个问题有更好的替代方案或解决方案吗?

根据答案,我应该更改XAML代码.我的是:

<UserControl x:Class="PcgTools.PcgWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:PcgTools.ViewModels" Height="Auto" Width="Auto" 
    Loaded="Window_Loaded">

<Grid>
  ...
    <RadioButton Content="_Programs" Height="16" HorizontalAlignment="Left" Margin="12,12,0,0" Name="radioButtonPrograms" VerticalAlignment="Top" 
                 IsChecked="{Binding Path=ProgramBanksSelected}" IsEnabled="{Binding Path=ProgramsEnabled}"/>
    <RadioButton Content="_Combis" Height="16" HorizontalAlignment="Left" Margin="85,12,0,0" Name="radioButtonCombis" VerticalAlignment="Top" 
                 IsChecked="{Binding Path=CombiBanksSelected}"  IsEnabled="{Binding Path=CombisEnabled}"/>
Run Code Online (Sandbox Code Playgroud)

我有两个(和更多)绑定到IsChecked的属性的Resharper问题(上面代码中的ProgramBanksSelected和CombiBanksSelected).

在答案中显示我应该使用DataTemplate,但我仍然无法弄清楚究竟是多么准确(没有使用MVVM light的Locator).

我应该如何使用数据上下文/模板?

Phi*_*hil 9

Resharper没有足够的信息来推断正在使用的setter.例如:

这段代码:

public partial class Page2
{
    public Page2()
    {
        InitializeComponent();

        DataContext = new List<ViewModel>
                          {
                              new ViewModel()
                          };
    }
}

public class ViewModel : ViewModelBase
{
    private bool _combiBanksSelected;
    public bool CombiBanksSelected
    {
        get { return _combiBanksSelected; }
        set
        {
            Set(()=>CombiBanksSelected, ref _combiBanksSelected, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用这个Xaml:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="Template" >
            <CheckBox IsChecked="{Binding CombiBanksSelected}"/>
        </DataTemplate>
    </Grid.Resources>
    <ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource Template}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

将显示设置器未使用(当SWA打开时).

但是,如果您将Xaml(将DataType ="{x:Type Samples:ViewModel}")更改为:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="Template" DataType="{x:Type Samples:ViewModel}">
            <CheckBox IsChecked="{Binding CombiBanksSelected}"/>
        </DataTemplate>
    </Grid.Resources>
    <ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource Template}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

R#现在有足够的信息,不会显示警告.

当然,还有其他方法可以为R#和VS Intellisense提供有关您正在使用的类型的更多提示.例如:

使用MVVM Light的视图定位器:

<UserControl ...
    DataContext="{Binding AViewModel, Source={StaticResource Locator}}" />
Run Code Online (Sandbox Code Playgroud)

使用d:DataContext.我建议看一下这个MSDN演练

<UserControl ...
    d:DataContext="{d:DesignInstance AViewModel, IsDesignTimeCreatable=true}"
Run Code Online (Sandbox Code Playgroud)

显式设置DataContext

<UserControl.Resources>
    <AViewModel x:Key="theModel/>
</UserControl.Resources>

<Grid DataContext="{StaticResource theModel}"> ...
Run Code Online (Sandbox Code Playgroud)

等等...

任何这些方法都允许R#和VS推断出类型的使用并提供智能感知.