PlaceholderText is not accessible XAML - wpf

I am new to XAML and WPF.
I am trying to add a placeholder to my textbox but I got this error: "The property 'PlaceholderText' was not found in type 'TextBox'" and "The member "PlaceholderText" is not recognized or is not accessible".
When I tried to type PlaceholderText, it didn't even show up in the auto-complete drop down list. "Header" has the problem.
I am using VS 2015 community and .NET Framework 4.5.2
Below is my code which I got it from https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.textbox#Examples
<Window x:Class="QuickGarbageSort.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:MyTestApp"
mc:Ignorable="d"
Title="MyTestApp" Height="550" Width="800">
<StackPanel>
<TextBlock Text="What's your name?"/>
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<TextBox x:Name="nameInput"
Header="Enter your name:" PlaceholderText="Name"
Width="300" HorizontalAlignment="Left"/>
<Button Content="Hello button" Click="Button_Click"/>
</StackPanel>
<TextBlock x:Name="greetingOutput"/>
</StackPanel>

The documentation you got the code from is a documentation for Windows.UI.Xaml.Controls.TextBox, this is the textbox control used in Universal Windows Applications. This has a property called PlaceholderText
The WPF one is System.Windows.Controls.TextBox, this is different and doesn't have a PlaceholderText property. In order to have something like the UWP placeholder text in WPF, you have to implement it yourself or use a third party library which offers a control with this functionality.

Related

Some Extended Unicode Characters (e.g., Zodiac Symbols) Displaying Differently between UWP and WPF

Certain extended Unicode characters, among which are the Zodiac symbols, are displaying differently in WPF TextBlocks vs. UWP TextBlocks. In UWP, as well as web browsers (for example, ♑), Toast messages, etc., display the characters with a purple background and white foreground. In WPF, the same characters are displayed with the control's background color and a black foreground. Ideally, the characters in WPF apps should be displayed consistently with the way they are displayed in other apps.
The following are the results of two test apps, one created as UWP and the other created as WPF. No changes were made to any of the other files (App.xaml, etc.), apart from the addition of the TextBlock control as follows:
UWP MainPage.xaml:
<Page x:Class="UwpApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpApp1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="150" Width="150">
<Grid>
<TextBlock Text="♑" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="48"/>
</Grid>
</Page>
UWP application screen:
WPF MainWindow.xaml:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="150">
<Grid>
<TextBlock Text="♑" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="48"/>
</Grid>
</Window>
WPF application screen:
Is anyone aware of an application setting or any other change that can be done to the WPF application so it will display these types of special characters consistently with other applications?

Don't understand why the user control we've written doesn't show up in a ModernWindow but does in a window

We're working on a WPF app, using the ModernUI for WPF. And we're also using MVVM Light. We've written some user controls which we intend to bring up in separate windows. Each of the windows are ModernUI's ModernWindow. Whenever we try to bring them up, nothing is shown in the ModernWindow. Out of frustration I thought I'd try doing the same thing in a plain old WPF window. Works perfectly each time. I don't know why it works in a regular WPF window but doesn't work in a ModernWindow. What is wrong???
Here's the XAML for the plain old window:
<Window x:Class="CoreFramework.BozoWindow"
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:view="clr-namespace:CoreFramework.View"
xmlns:local="clr-namespace:CoreFramework"
mc:Ignorable="d"
Title="Bozo Window" Height="300" Width="300">
<ContentControl Content="{Binding ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
And here's the XAML for the ModernWindow:
<mui:ModernWindow x:Class="CoreFramework.DataViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:vm="clr-namespace:CoreFramework.ViewModel"
xmlns:view="clr-namespace:CoreFramework.View"
xmlns:content="clr-namespace:CoreFramework"
Title="BOTS"
ShowInTaskbar="True"
Closing="ModernWindow_Closing">
<ContentControl Content="{Binding Path=ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
One approach you could use is adding Menu links which is built into the Modern UI framework:
<mui:ModernWindow x:Class="ExampleApp.ExampleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="Scrum Manager" IsTitleVisible="True" ContentSource="/Views/DefaultUserControl.xaml"
x:Name="mainWindow" MenuLinkGroups="{Binding Groups}"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<i:Interaction.Triggers>
</i:Interaction.Triggers>
<mui:ModernWindow.TitleLinks>
<mui:Link DisplayName="settings" Source="/Views/SettingsWindow.xaml" />
<mui:Link DisplayName="Sign out" Source="/Infrastructure/Logout.xaml" />
</mui:ModernWindow.TitleLinks>
When a link is clicked, it opens up the 'Source' which would be UserControl xaml files.
See Github for more details:
https://github.com/firstfloorsoftware/mui

How to see design-time data-binding in XAML editor (it works in runtime)?

I data-binded version number to appear as follows:
<Window <!-- ... --> DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<TextBlock>
Version is:
<Run Text="{Binding Version, Mode=OneWay}"></Run>
and advancing...
</TextBlock>
</Grid>
</Window>
and it's working during run-time.
How can I see it during design-time in the XAML editor in Visual Studio 2012 ? I only see:
Version is: and advancing...
instead of:
Version is: 5.2.2 and advancing...
EDIT - My solution:
Jure's answer below works, but I ended up using a dummy view-model static code technique, which works better for me since the data is a mock of the real view-model type:
d:DataContext="{Binding Source={StaticResource DesignViewModel}}" ...
Make sure that you have these definitions at the root tag of your xaml file (in your case the Window tag):
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Then, anywhere in the xaml (including the root tag) you can add this:
d:DataContext="{d:DesignInstance myNamespace:MyViewModel, IsDesignTimeCreatable=True}"
Now you just need to make sure that you initialize the values in a constructor or have default values for properties.
If you need to run a special logic for design mode, look at this answer.
Short answer, you can't do it that way. VS designer is not executing runtime code and your binding will not be resolved in design time.
But there is support for design time data through d:DesignData extension.
You can set design data context this way:
<Window xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DataContext="{d:DesignData Source=/SampleData/SomeSampleData.xaml}"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<TextBlock>
Version is:
<Run Text="{Binding Version, Mode=OneWay}"></Run>
and advancing...
</TextBlock>
</Grid>
d:DataContext={d:DesignData.... sets the desing time DataContext that will be used to resolve bindings in VS designer surface. You can set it to a xaml file that contains your sample data. Sample xaml file should be built with "DesignData" build action.
See more here: http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx

Flowdocument and Databinding

After thoroughly reading the documentation on flowdocuments (in WPF) it seems as though flowdocument does not easily support databinding. PLEASE say this is not true! I have a listbox using the sample data in Expression Blend and I inserted a textblock into the flowdocument. The textblock (text property) is databinded to the string data in the listbox. After running the project I would expect that the textblock text changes as the listbox selection changes, but nothing happens. Databinding is not working. What is the easiest way to make databinding work with flowdocument?
Here is the XAML.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<StackPanel>
<TextBlock Text="{Binding Property1}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<FlowDocumentScrollViewer Margin="120,64,256,126">
<FlowDocument>
<Paragraph><Run Text="Only A Test"/><InlineUIContainer>
<TextBlock TextWrapping="Wrap" Text="{Binding Collection[0].Property1}" Height="56" Width="112"/>
</InlineUIContainer></Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
<ListBox ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="0,83.847,52,62.153" HorizontalAlignment="Right" Width="200"/>
</Grid>
</Window>
According to this Microsoft documentation in April 2009, this is not possible:
While there are many great features in flow documents, if your documents are generated from dynamic data, you have a bit of a problem: there is no support for data binding in flow documents. The flow document elements (Section, Table, Run, Paragraph and the like) are dependency objects, but don't define any dependency properties that would allow you to dynamically change or generate content.

How can I center the title of a WPF RibbonWindow?

I want to center the title of my RibbonWindow, and not have it aligned on the side.
This thread said it had an answer:
Center WPF RibbonWindow Title via XAML Code
but it didn't work.
Bellow is an image and the corresponding code.
<RibbonWindow x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Ribbon>
<Ribbon.TitleTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center" HorizontalAlignment="Stretch" Width="{Binding ElementName=Window, Path=ActualWidth}">ApplicationTitle
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="0" Color="MintCream " BlurRadius="10" />
</TextBlock.Effect>
</TextBlock>
</DataTemplate>
</Ribbon.TitleTemplate>
</Ribbon>
</Grid>
</RibbonWindow>
I am using VS 2012, with .NET 4.5 and the included System.Windows.Controls.Ribbon assembly.
Had a bad experience with using RibbonControlsLibrary for WPF. It has issues not only around title centering. It also breaks window rounding corners at the top, icon and title goes out of the screen when maximized, and personally I haven't found any chance to program ribbon group dialog. All of this lead me to searching for an alternative, and I have found a Fluent Ribbon Controls Suite
Download source code and build it for .NET 4.5 (I did it with absolutely no issues).
The ElementName in the binding (Width="{Binding ElementName=Window, Path=ActualWidth}") needs to match the name of the RibbonWindow. So, in this case you need "Window" for the name:
<RibbonWindow x:Class="Window1" x:Name="Window"
... />

Resources