如何更改切换按钮 IsChecked 上 属性 的值?
How to change the value of a property on toggle button IsChecked?
在我的基地 class 中,我有以下内容:
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";
}
}
我正在使用 TextBlock 在我的 XAML 中显示 GroupOperator。理想的功能是根据 Toggle Button 是否被切换,让字符串值在 And / Or 之间变化。现在,它根本没有改变 TextBlock,我想知道我做错了什么。
这是我的 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>
注意:ParentGroup.GroupOperator 和 GroupOperator 在正确处理 PropertyChange 时实际上应该是相同的值。
你的代码应该是
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");
}
}
因为设置运算符值改变了不止一个 属性 你需要在它改变时通知更多属性改变
在我的基地 class 中,我有以下内容:
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";
}
}
我正在使用 TextBlock 在我的 XAML 中显示 GroupOperator。理想的功能是根据 Toggle Button 是否被切换,让字符串值在 And / Or 之间变化。现在,它根本没有改变 TextBlock,我想知道我做错了什么。
这是我的 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>
注意:ParentGroup.GroupOperator 和 GroupOperator 在正确处理 PropertyChange 时实际上应该是相同的值。
你的代码应该是
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");
}
}
因为设置运算符值改变了不止一个 属性 你需要在它改变时通知更多属性改变