Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:40:51+00:00 2026-05-17T16:40:51+00:00

I’m trying to learn how to separate a view from its associated viewmodel, while

  • 0

I’m trying to learn how to separate a view from its associated viewmodel, while making the view have as little or no code-behind as possible.

my control has a textblock when the object is in a display mode, and a textbox when the user wants to edit that field. In both cases, these controls must bind to the same string in the modelview, but only the appropriate one should be displayed depending on the state of the viewmodel. Previously, I’d just modify the panels child to a new element in the codebehind… but as I understand it, I should be trying to make all my changes in XAML for the view.

My viewmodel has a bool denoting if its in display or edit mode. Is there a way to specify use of a different template depending on the value of that bool, but keep it all in XAML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:40:52+00:00Added an answer on May 17, 2026 at 4:40 pm

    There is a way to do what you say you’re working on, by using DataTriggers.

    First, define a Style which contains the DataTriggers you want to use. For example, note here two identical styles for a ContentControl, each with a DataTrigger pair that performs the opposite of the other:

    <Window.Resources>
         <Style TargetType="{x:Type ContentControl}" x:Key="HiddenWhenFalse" >
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger  Value="False" Binding="{Binding MyBooleanValue}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger  Value="True" Binding="{Binding MyBooleanValue}">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    
    
        <Style TargetType="{x:Type ContentControl}" x:Key="HiddenWhenTrue" >
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger  Value="True" Binding="{Binding MyBooleanValue}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger  Value="False" Binding="{Binding MyBooleanValue}">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    

    Then, in your main visual tree you would define ContentControls which use those Styles, and assign the DataContext of the Window or UserControl or whatever to your ViewModel. Something like this:

    <Grid>
        <StackPanel >
            <Button Content="False" Name="Button2"></Button>
            <Button Content="True" Name="Button1"></Button>
            <ContentControl Style="{StaticResource HiddenWhenFalse}">
                <ContentControl.Content>
                    <TextBlock Text="ITS ALL TRUE"/>
                </ContentControl.Content>
            </ContentControl>
            <ContentControl Style="{StaticResource HiddenWhenTrue}">
                <ContentControl.Content>
                    <TextBlock Text="ITS ALL FALSE"/>
                </ContentControl.Content>
            </ContentControl>
        </StackPanel>
    </Grid>
    

    Here is the ViewModel I’m using, note the implementation of INotifyPropertyChanged:

    Imports System.ComponentModel
    
    Public Class MainWindowViewModel
        Implements INotifyPropertyChanged
        Private _MyBooleanValue = False
        Public Property MyBooleanValue
            Get
                Return _MyBooleanValue
            End Get
            Set(ByVal value)
                _MyBooleanValue = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Nothing))
            End Set
        End Property
    
        Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Class
    

    Now, for the purposes of this sample, I simply wired up the two buttons to set the ViewModel value. If you want to use Commanding to wire up buttons, that’s an entirely different topic. It’s worth discussing, but for the sake of simplicity:

    Class MainWindow 
        Private vm As New MainWindowViewModel
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
            vm.MyBooleanValue = True
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
            vm.MyBooleanValue = False
        End Sub
    
        Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            Me.DataContext = vm
        End Sub
    End Class
    

    Bear in mind that this sample explicitly styles a ContentControl, and that you’ll have to change the TargetType of your style if you’re working with a type that isn’t descended from that class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.