0
<ItemsControl ItemsSource="{Binding Tariffs}" Margin="6">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <custControls:RoundButton Name="TariffButton" Margin="3"
         Content="{Binding TariffName}" Style="{DynamicResource TariffButton}">
           <i:Interaction.Triggers>
              <i:EventTrigger EventName="Click">
                 <cal:ActionMessage MethodName="PickUpTariff">
                   <cal:Parameter Value="{Binding Path=Content, 
                       RelativeSource={RelativeSource AncestorType={x:Type custControls:RoundButton}}}" />
                 </cal:ActionMessage>
              </i:EventTrigger>
           </i:Interaction.Triggers>
       </custControls:RoundButton>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

相对源绑定工作得很好。Content="{Binding TariffName}" 仅适用于。

但这是 Style TariffButton 中包含的内容。

<Style x:Key="TariffButton" TargetType="customControls:RoundButton" BasedOn="{StaticResource {x:Type Button}}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsChosen, Mode=OneWay}" Value="True">
            <Setter Property="Background" Value="Green"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsChosen, Mode=OneWay}" Value="False">
            <Setter Property="Background" Value="Blue"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

它不起作用!更准确地说,它只工作一次。所以数据触发器只被调用一次。

这是 ViewModel 部分:

 public class DocumentChoiceViewModel:PropertyChangedBase {
    public ObservableCollection<Tariff> Tariffs { get; private set; }

    public DocumentChoiceViewModel() {
        Tariffs = new ObservableCollection<Tariff> {
            new Tariff {IsChosen = true, TariffName = "ПАССАЖИРСКИЙ"},
            new Tariff {IsChosen = false, TariffName = "ЭКСПРЕСС"},
            new Tariff {IsChosen = false, TariffName = "КОМФОРТ"},
            new Tariff {IsChosen = false, TariffName = "ДОП.СК.М-НОГИНСК"}
        };
    }      

    public void PickUpTariff(string tariffName) {
        if (!IsTariffPlanExists(tariffName))
            throw new InvalidOperationException(
                "Unexpectable state. User should not be able to choose tariff plan which is not present in Tariffs list");                     
    }        
}

public class Tariff:PropertyChangedBase {
    private bool isChosen;

    public bool IsChosen {
        get { return isChosen; }
        set {
            isChosen = value;
            NotifyOfPropertyChange(()=>IsChosen);
        }
    }

    public string TariffName { get; set; }
}
4

3 回答 3

0

样式触发器仅适用于 TargetType 中定义的依赖属性。

你为什么不做一个 IsChosenToBackgroundConverter 并写这个

<RoundButton Background="{Binding IsChosen, Converter={StaticResource IsChosenToBackgroundConverter}"/>
于 2013-11-08T09:05:23.957 回答
0

我不知道这是否会解决您的问题,但您不需要TriggerTrue and False条件添加 a:

<Style x:Key="TariffButton" TargetType="customControls:RoundButton" 
BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Background" Value="Blue" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsChosen}" Value="True">
            <Setter Property="Background" Value="Green" />
        </DataTrigger>
    </Style.Triggers>
</Style>
于 2013-11-08T10:35:06.313 回答
0

因此,首先,如果您在调试绑定时遇到问题 - 使用转换器作为附加层,您将在其中看到所有更改。
和第二套绑定方式要一样TwoWay

于 2013-11-08T09:04:22.120 回答