2015-07-17 3 views
0

У меня есть список тяжелых Controls, которые я не хочу отображать, прежде чем пользователь взаимодействует с ними (по одному за раз). Я хочу показать заполнитель для каждого Control до тех пор, пока не будет нажата кнопка-заполнитель (желательно сфокусирована), а затем визуализируется реальная Control.WPF показать «фиктивный» контроль до тех пор, пока не сфокусирован/не нажат

То, что я пытался выглядеть следующим образом:

  <ContentControl x:Name="theControl"> 
      <TextBox x:Name="TextBlock" Text="Placeholder right here."/> 
      <ContentControl.Style> 
       <Style TargetType="ContentControl"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding IsFocused, ElementName=TextBlock}" Value="True"> 
          <Setter Property="Content" > 
           <Setter.Value> 
            <Grid x:Name="theGrid"> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="Auto" SharedSizeGroup="CodeColumn"/> 
              <ColumnDefinition Width="*"/> 
             </Grid.ColumnDefinitions> 
             <TextBlock>Heavy control part1</TextBlock> 
             <TextBlock Grid.Column="1">heavy control part2</TextBlock> 
            </Grid> 
           </Setter.Value> 
          </Setter> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </ContentControl.Style> 
     </ContentControl> 

Любой знает, лучший подход или то, что я не хватает?

ответ

0

Это общая идея о том, что мне удалось получить работу.

public partial class PlaceHolder : UserControl 
{ 
    private bool m_isReadOnly; 
    private object m_PlaceholdeContent; 
    private bool m_hasValue; 
    private object m_realContent; 

    public PlaceHolder() 
    { 
     InitializeComponent(); 
     GotFocus += OnGotFocus; 
    } 

    private void OnGotFocus(object sender, RoutedEventArgs routedEventArgs) 
    { 
     GotFocus -= OnGotFocus; 
     if (!RealContentIsUsed) 
     { 
      RealContentIsUsed = true; 
      Content = RealContent; 
     } 
    } 

    private bool RealContentIsUsed { get; set; } 

    public object RealContent 
    { 
     get { return m_realContent; } 
     set 
     { 
      m_realContent = value; 
      if (IsReadOnly || HasValue) 
      { 
       Content = m_realContent; 
      } 
     } 
    } 

    public object PlaceholdeContent 
    { 
     get { return m_PlaceholdeContent; } 
     set 
     { 
      m_PlaceholdeContent = value; 
      if (!RealContentIsUsed) 
      { 
       Content = m_PlaceholdeContent; 
      } 
     } 
    } 

    public bool IsReadOnly 
    { 
     get { return m_isReadOnly; } 
     set 
     { 
      m_isReadOnly = value; 
      if (value && !RealContentIsUsed) 
      { 
       Content = RealContent; 
       RealContentIsUsed = true; 
      } 
     } 
    } 

    public bool HasValue 
    { 
     get { return m_hasValue; } 
     set 
     { 
      m_hasValue = value; 
      if (HasValue && RealContentIsUsed == false) 
      { 
       Content = RealContent; 
       RealContentIsUsed = true; 
      } 
     } 
    } 
} 
1

Не знаю, если это решение лучше, но вы можете создать тяжелый контроль в коде, а затем удалить/добавить детей после события GotFocus.

Добавить событие GotFocus к вашему TextBlock и поставить TextBlock в сетке

 <Grid Name="myGrid"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="100*" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="100*"/> 
      </Grid.RowDefinitions> 
      <TextBox x:Name="TextBlock" Grid.Column="0" Grid.Row="0" Text="Placeholder right here." GotFocus="TextBlock_GotFocus" /> 
     </Grid> 

Затем в файле CS

private void TextBlock_GotFocus(object sender, RoutedEventArgs e) 
    { 
     createNewControl(); 
    } 

    private void createNewControl() 
    { 
     Grid myOtherGrid = new Grid(); 

     RowDefinition newRow1 = new RowDefinition(); 
     newRow1.Height = new GridLength(100.0); 
     RowDefinition newRow2 = new RowDefinition(); 
     newRow2.Height = new GridLength(100.0); 

     ColumnDefinition newColumn1 = new ColumnDefinition(); 
     newColumn1.Width = new GridLength(50.0); 
     ColumnDefinition newColumn2 = new ColumnDefinition(); 
     newColumn2.Width = new GridLength(50.0); 

     myOtherGrid.RowDefinitions.Add(newRow1); 
     myOtherGrid.RowDefinitions.Add(newRow2); 
     myOtherGrid.ColumnDefinitions.Add(newColumn1); 
     myOtherGrid.ColumnDefinitions.Add(newColumn2); 

     TextBox myOtherTextBlock1 = new TextBox(); 
     myOtherTextBlock1.Text = "new block 1"; 

     TextBox myOtherTextBlock2 = new TextBox(); 
     myOtherTextBlock2.Text = "new block 1"; 

     myOtherGrid.Children.Add(myOtherTextBlock1); 
     Grid.SetRow(myOtherTextBlock1, 0); 
     Grid.SetColumn(myOtherTextBlock1, 0); 

     myOtherGrid.Children.Add(myOtherTextBlock2); 
     Grid.SetRow(myOtherTextBlock2, 1); 
     Grid.SetColumn(myOtherTextBlock2, 1); 

     myGrid.Children.Remove(TextBlock); 
     myGrid.Children.Add(myOtherGrid); 
    } 
+0

Следует отметить, что я удалил все элементы управления контентом из исходного примера xml и просто имел сетку, содержащую оригинальный TextBlock. – Rhubarb

+0

Kinda, работает, хотя я бы хотел, чтобы мой взгляд был в свойствах. (Это было в моем ответе) – Illedan

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