简体   繁体   中英

I added style for dataGridCell, but when I try to find it, exception is appearing that style didn't find

In the code there are two styles which I want toggle in the DataGridColumn "CreationDateDisplayer". DataGrid have two conditions which are described by the type CurrentDisplayingListTypes. Method ToggleCurrentDisplayingList successfully change condition of DataGrid.

I want change column styles when DataGrid condition is changed. I try to change style in the code "CreationDateDisplayer.CellStyle = Application.Current.FindResource("Default") as Style;", but "FindResourse" don't find style.

(I removed code which can't be related with my problem)

        <Window.Resources> 
               <Style TargetType="DataGridCell" x:Key="Default">
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
                <Style TargetType="DataGridCell" x:Key="CreationDate" BasedOn="{StaticResource Default}" x:Name="RecordingList">
                    <Setter Property="Foreground" Value="Green"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="#d92525"/>
                            <Setter Property="Background" Value="White"/>
                            <Setter Property="BorderBrush" Value="White"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Window.Resources>
            <Grid>
               //some button which method is "ToggleCurrentDisplayingList"
               <DataGrid x:Name="RecordingsDisplayer">
                    <DataGridTextColumn x:Name="CreationDateDisplayer" CellStyle="{StaticResource CreationDate}"/>
            <Grid>
    </Window.Resources>
     
  
        private void ToggleCurrentDisplayingList(object sender, RoutedEventArgs e)
        {
            switch (currentDisplayingList)
            {
                case CurrentDisplayingListTypes.RecordingList:                   
                    RecordingsDisplayer.ItemsSource = basket;                    
                    CreationDateDisplayer.CellStyle = Application.Current.FindResource("Default") as Style;
                    currentDisplayingList = CurrentDisplayingListTypes.Basket;
                    break;
                case CurrentDisplayingListTypes.Basket:          
                    RecordingsDisplayer.ItemsSource = recordingList;
CreationDateDisplayer.CellStyle = Application.Current.FindResource("CreationDate") as Style;
                    currentDisplayingList = CurrentDisplayingListTypes.RecordingList;
                    break;
            } 
        }

Application.Current.FindResource("Default") will perform search starting from Application.Resources.

By the look of things, "Default" Style is defined close to DataGrid, so use DataGrid's own FindResource method

CreationDateDisplayer.CellStyle = RecordingsDisplayer.FindResource("Default") as Style;

If the resource is not found on the calling element, the parent element in the logical tree is searched next, then the application, then themes, and finally system resources.

since ge it directly from window Resources:

CreationDateDisplayer.CellStyle = this.Resources["Default"] as Style;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM