1

私はコードを持っています:

    <UserControl x:Class="MediaNet.View.MusicWindow.MusicWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:musicVM="clr-namespace:MediaNet.ViewModel.MusicWindowViewModel"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                 mc:Ignorable="d"
                 d:DesignHeight="350" d:DesignWidth="557">
        <UserControl.DataContext>
            <musicVM:MusicWindowViewModel />
        </UserControl.DataContext>
        <UserControl.Resources>
            <musicVM:TimeSpanConverter x:Key="TimeSpanConverter" />
            <musicVM:CurrentSongIndexConverter x:Key="CurrentSongIndexConverter" />
        </UserControl.Resources>
             <DataGrid Grid.Row="1" AutoGenerateColumns="True" VerticalAlignment="Top"  ItemsSource="{Binding Path=MusicItems}" SelectedIndex="{Binding Path=SelectedIndex}" >
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}, RelativeSource={RelativeSource Mode=Self}}"  Value="True">
                        <Setter Property="Background"  Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.ContextMenu>
            <ContextMenu >
                <MenuItem Command="Delete">
                    <MenuItem.Icon>
                        <Image  />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Song options">
                    <MenuItem Header="Play to this song" Command="{Binding SetStopPositionCommand}"  />
                </MenuItem>
            </ContextMenu>
        </DataGrid.ContextMenu>
    </DataGrid>

MusicItemは

ObservableCollection<Song> 

ビューモデル:

namespace MediaNet.ViewModel.MusicWindowViewModel
{
    public class MusicWindowViewModel : INotifyPropertyChanged, IDisposable
    {
 #region CurrentSongIndex
        private int _currentSongIndex;
        public int CurrentSongIndex
        {
            get { return _currentSongIndex; }
            set
            {
                _currentSongIndex = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrentSongIndex"));
                }
            }
        }
        #endregion
 }
}

コンバータ:

namespace MediaNet.ViewModel.MusicWindowViewModel
{
    class CurrentSongIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int CurrentSongIndex = (int)value;
            return CurrentSongIndex > 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

これにより、背景色がデータグリッドの行に設定されますが、機能するようになりました。背景を変更する必要がある行をトリガーに指示できますか?

4

1 に答える 1

1

はのStyleすべての行に適用されますDataGrid。のは、各行のに対して相対的BindingであるDataTrigger必要があります。DataContextこれにより、行ごとにバインディングが評価されます。

次の点を明確にして確認してください。

  • これはどのように正確に「機能しない」のですか?強調表示されている行はなく、すべての行が強調表示されていますか?
  • あなたのコンバーターは動作しますか?trueトリガーバインディングを適切に評価することが期待されるときに返されることを確認しましたか?

アップデート

更新されたコード サンプルを見ると、問題はeach にCurrentSongIndexないことです。XAML ごとに、.DataContextDataGridRowItemsSource="{Binding Path=MusicItems}"

グリッドの各行がデータバインドされている場合、DataGridRow.DataContextは対応する に設定されSongます。CurrentSongIndexその場合、バインディングはの一部であるため、にアクセスできなくなりますMusicWindowViewModel

データ トリガー バインディングを次のように変更してみてください。

{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}}

これにより、バインディングは、プロパティを含むDataContextウィンドウの を参照するように強制されます。DataContextMusicWindowViewModelCurrentSongIndex

于 2012-07-26T20:57:32.547 に答える