我认为这是WFP中有关初学者的问题。 我有一个用户控件,它使用两个普通按钮充当单选按钮: (按钮更改颜色以显示当前选择)
messsage
后面的代码由2个依赖项属性Title和IsYes组成。 使用单击事件单击按钮时,将修改IsYes属性。
<UserControl x:Class="UI.UserControls.RadioUC" Loaded="UserControl_Loaded">
<Stackpanel DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
<TextBlock Text="{Binding Path=Title}"/>
<Button x:Name="BtnYes" Content="YES" Click="BtnYes_Click">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding Command}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="BtnNo" Content="NO" Click="BtnNo_Click">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding Command}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Stackpanel>
</UserControl>
最后这就是我的用法:
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(RadioUC),
new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static readonly DependencyProperty IsYesProperty = DependencyProperty.Register("IsYes", typeof(bool), typeof(RadioUC),
new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public bool IsYes {
get { return (bool)GetValue(IsYesProperty); }
set { SetValue(IsYesProperty, value); }
}
public String Title {
get { return (String)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
private void BtnYes_Click(object sender, RoutedEventArgs e)
{
IsYes = true;
//BtnYes.Background = new SolidColorBrush(Colors.ForestGreen);
//BtnNo.Background = new SolidColorBrush(Colors.Gray);
}
private void BtnNo_Click(object sender, RoutedEventArgs e)
{
IsYes = false;
//BtnNo.Background = new SolidColorBrush(Colors.ForestGreen);
//BtnYes.Background = new SolidColorBrush(Colors.Gray);
}
单独单击按钮时,将成功修改usercontrol的IsYes属性。没有问题。问题是,当我在用户控件的托管窗口中以编程方式修改IsLocalYes时,此更改不会传播到用户控件中。所以问题是,当IsLocalYes属性更改时,如何修改用户控件的IsYes属性?
答案 0 :(得分:0)
为了创建自己的目标,与其创建自己的UserControl
,不如使用ToggleButton
并更改ControlTemplate
/ Style
。
也请阅读此article
解决方案:
ToggleButton样式:
<Style x:Key="YesNoToggleButtonStyle" TargetType="ToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Grid x:Name="PART_GRIDYES" Background="Gray" MinWidth="6" MinHeight="6">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">YES</TextBlock>
</Grid>
<Grid x:Name="PART_GRIDNO" Background="Gray" MinWidth="6" MinHeight="6">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">NO</TextBlock>
</Grid>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="PART_GRIDYES" Property="Background" Value="ForestGreen"></Setter>
</Trigger>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="PART_GRIDNO" Property="Background" Value="ForestGreen"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
像这样使用它:
<ToggleButton Style="{StaticResource YesNoToggleButtonStyle}" IsChecked="{Binding IsLocalYes, Mode=TwoWay}">Is it YES</ToggleButton>