DataBinding不起作用。 System.Windows.Data跟踪不提供任何有用的信息

时间:2013-11-08 08:55:57

标签: c# wpf xaml binding

<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; }
}

3 个答案:

答案 0 :(得分:0)

首先,如果您在调试绑定时遇到一些问题 - 请使用转换器作为附加层,您将看到所有更改 第二 - 将绑定模式设置为TwoWay

答案 1 :(得分:0)

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

你为什么不制作一个IsChosenToBackgroundConverter并写下这个

<RoundButton Background="{Binding IsChosen, Converter={StaticResource IsChosenToBackgroundConverter}"/>

答案 2 :(得分:0)

我不知道这是否能解决您的问题,但您无需为Trigger True条件添加False

<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>