2017-02-20 28 views
0

Этот вопрос связан с удалением атрибута в xaml. В приведенном ниже коде у меня есть Span. Во время мероприятия я добавляю задний план в этот Span. Во время другого мероприятия мне нужно удалить его. Пожалуйста, дайте мне знать, если есть способ удалить атрибут фона, который я установил в Span.Удалить атрибут из xaml с помощью C# - WPF

Мой Xaml выглядит следующим образом.

<Window x:Class="WpfApplication4.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApplication4" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
     <RowDefinition Height="40"></RowDefinition> 
    </Grid.RowDefinitions> 
    <RichTextBox x:Name="rtb"> 
     <FlowDocument> 
      <Paragraph> 
       <Span x:Name="def" Tag="default"> 
        <Run x:Name="deg">Some Text</Run> 
       </Span> 
      </Paragraph> 
     </FlowDocument> 
    </RichTextBox> 
    <TextBox x:Name="tx" Grid.Row="1" TextWrapping="Wrap"/> 
    <Grid Grid.Row="2"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Button Content="s" Click="Button_Click"/> 
     <Button Content="bg-Add" Grid.Column="1" Click="Button_Click"/> 
     <Button Content="bg-Remove" Grid.Column="2" Click="Button_Click"/> 
    </Grid> 
</Grid> 

</Window> 

Мой код выглядит следующим образом.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Markup; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows. Shapes; 

namespace WpfApplication4 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     if ((sender as Button).Content.ToString() == "s") 
     { 
      tx.Text = XamlWriter.Save(def); 
     } 
     else if ((sender as Button).Content.ToString() == "bg-Add") 
     { 
       def.Background = new SolidColorBrush(Colors.Blue); 
     } 
     else if ((sender as Button).Content.ToString() == "bg-Remove") 
     { 
      //Need to remove the set back color so that I get the default back 
     } 
    } 
} 
} 

ответ

1

Я могу найти ответ самостоятельно.

def.ClearValue(Span.BackgroundProperty); 

собирается удалить этот атрибут для меня.

+1

Просто к сведению. То, что вы называете атрибутом, на самом деле является свойством или, точнее, свойством зависимости WPF. Вот почему вы можете вызвать ClearValue. Если это было обычное свойство CLR, оно все равно было бы установлено в XAML, но не было бы метода ClearValue. – Clemens

0

Вы можете переопределить ShouldSerializeProperty

public class CustomizedSpan : Span 
{ 

    public bool IsRemoveBackGround; 

    protected override bool ShouldSerializeProperty(DependencyProperty dp) 
    { 
     if (dp == Span.BackgroundProperty && IsRemoveBackGround) 
     { 
      return false; 
     } 
     else 
     { 

      return base.ShouldSerializeProperty(dp); 
     } 
    } 
} 
+0

Благодарим вас за этот вариант. Что-то новое, что я узнал сегодня. – Naresh

 Смежные вопросы

  • Нет связанных вопросов^_^