在我的基类中,我有以下内容:
private string groupOperator;
public string GroupOperator
{
get
{
this.groupOperator = ConvertOperatorToString();
return this.groupOperator;
}
set
{
this.groupOperator = value;
OnPropertyChanged("GroupOperator");
}
}
private bool isOrOperator = false;
public bool IsOrOperator
{
get
{
return this.isOrOperator;
}
set
{
this.isOrOperator = value;
OnPropertyChanged("IsOrOperator");
}
}
public string ConvertOperatorToString()
{
if (IsOrOperator)
{
return "Or";
}
else
{
return "And";
}
}
我正在使用文本块在我的 XAML 中显示组运算符。理想的功能是根据是否切换切换按钮,在 And/Or 之间更改字符串值。现在,它根本没有更改文本块,我想知道我做错了什么。
这是我的 XAML:
<TextBlock Style="{StaticResource TextBlockBaseStyling}" VerticalAlignment="Center" Text="{Binding GroupOperator, UpdateSourceTrigger=PropertyChanged}"/>
<ToggleButton x:Name="OperatorSwitch" Style="{StaticResource ToggleViewSwitch}"
Visibility="{Binding IsToggleVisible, UpdateSourceTrigger=PropertyChanged}"
IsChecked="{Binding IsOrOperator, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Grid>
</Grid>
<ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Border>
</Grid>
<TextBlock Style="{StaticResource TextBlockBaseStyling}"
Text="{Binding ParentGroup.GroupOperator, UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding IsOperatorVisible, Converter={StaticResource BooleanConverter}, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0, 10, 0, 0"/>
</StackPanel>
注意:当正确处理 PropertyChange 时,ParentGroup.GroupOperator 和 GroupOperator 实际上应该是相同的值。
你的代码应该是
public string GroupOperator
{
get
{
return ConvertOperatorToString();
}
}
private bool isOrOperator = false;
public bool IsOrOperator
{
get
{
return this.isOrOperator;
}
set
{
this.isOrOperator = value;
OnPropertyChanged("IsOrOperator");
OnPropertyChanged("GroupOperator");
}
}
当设置运算符值更改多个属性时,您需要在更改时通知更多属性更改