1

考虑下面的 XAML =>

        <fluent:Ribbon x:Name="MenuRibbon"
                       Title="title"
                       SnapsToDevicePixels="True">
        <fluent:RibbonTabItem x:Name="Home"
                                  Header="Home">
            <fluent:RibbonGroupBox Header="Project">
                <fluent:InRibbonGallery MinItemsInRow="3"
                                            MaxItemsInRow="6"
                                            Width="300"
                                            ItemWidth="64"
                                            ItemHeight="56"
                                            ItemsSource="{Binding Projects}">
                    <fluent:InRibbonGallery.ItemTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <Border BorderBrush="{x:Null}"  Height="56">

                                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" DockPanel.Dock="Bottom"
                                               Text="{Binding Name}">
                                    </TextBlock>
                                </Border>
                            </DockPanel>
                        </DataTemplate>
                    </fluent:InRibbonGallery.ItemTemplate>
                </fluent:InRibbonGallery>
            </fluent:RibbonGroupBox>
        </fluent:RibbonTabItem>
    </fluent:Ribbon>

我已经将项目的 ObservableColleciton 绑定到 InRibbonGallery ,并且 ViewModel 中存在一个项目实例(ActiveProject)。
在 DataTemplate 中定义的 TextBlock,用于显示项目对象的名称。
如何更改包含 Active Project 的 TextBlock 的颜色?
视图模型:

public class ViewModel : ViewModelBase
{
    private ObservableCollection<Project> _projects;

    public ViewModel()
    {
        Projects = new ObservableCollection<Project>(new List<Project>
                                                         {
                                                             new Project {Id = "0", Name = "Project1"},
                                                             new Project {Id = "1", Name = "Project2"},
                                                             new Project {Id = "2", Name = "Project3"}
                                                         });
        Project = Projects[0];
    }

    public ObservableCollection<Project> Projects
    {
        get { return _projects; }
        set
        {
            _projects = value;
            RaisePropertyChanged(() => Projects);
        }
    }

    public Project Project { get; set; }
}

public class Project : ObservableObject
{
    private string _id;
    private string _name;

    public string Name

    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    public string Id
    {
        get { return _id; }
        set
        {
            _id = value;
            RaisePropertyChanged(() => Id);
        }
    }
}

项目文件位于此处

4

1 回答 1

1

您可以通过结合使用 aMultiBinding和 aDataTrigger来获得更好的结果。

所以你的 xaml 看起来像:

<Window.Resources>
  <vm:ViewModel x:Key="ViewModel" />
  <vm:ActiveProjectCheckConverter x:Key="ActiveProjectCheckConverter" />
</Window.Resources>
...
<TextBlock HorizontalAlignment="Center"
            VerticalAlignment="Center"
            DockPanel.Dock="Bottom"
            Text="{Binding Name}">
  <TextBlock.Style>
    <Style TargetType="{x:Type TextBlock}">
      <Setter Property="Background"
              Value="Transparent" />
      <Style.Triggers>
        <DataTrigger Value="True">
          <DataTrigger.Binding>
            <MultiBinding Converter="{StaticResource ActiveProjectCheckConverter}">
              <Binding Path="Name" />
              <Binding Path="DataContext.ActiveProject.Name"
                        RelativeSource="{RelativeSource FindAncestor,
                                                        AncestorType={x:Type fluent:InRibbonGallery}}" />
            </MultiBinding>
          </DataTrigger.Binding>
          <Setter Property="Background">
            <Setter.Value>
              <LinearGradientBrush StartPoint="1,0">
                <GradientStop Offset="0.0"
                              Color="#00FFFFFF" />
                <GradientStop Offset="1.1"
                              Color="#FFFFFFFF" />
              </LinearGradientBrush>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </TextBlock.Style>
</TextBlock>

和你的转换器:

public class ActiveProjectCheckConverter : IMultiValueConverter {
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
    string first = values[0] as string;
    string second = values[1] as string;
    return !string.IsNullOrEmpty(first) && !string.IsNullOrEmpty(second) && first == second;
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
    throw new NotImplementedException();
  }
}

现在我又做了一个更改,您的 ViewModel 中的 Project 对象,如果您希望对它的更改反映在 View 中,您需要自己实现 INPC。所以我确实更新了它并将其重命名为ActiveProject

private Project _activeProject;

public Project ActiveProject {
  get {
    return _activeProject;
  }
  set {
    if (value == _activeProject)
      return;
    _activeProject = value;
    RaisePropertyChanged(() => ActiveProject);
  }
}

更新

您可以在以下位置找到上述更新:Dropbox-Link

于 2013-04-23T21:15:40.297 回答