Hi i have the following window, it has a button, and when i click on it, it shows a popup that contains some text. When the popup is closed, the behaviour cleans the text inside the popup
<Window
x:Class="WpfTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviours="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="300"
mc:Ignorable="d">
<StackPanel
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Red"
Orientation="Vertical">
<ToggleButton
x:Name="CustomButton"
Width="40"
Height="40"
Content="Checkbutton" />
<Popup
IsOpen="{Binding ElementName=CustomButton, Path=IsChecked}"
Placement="Bottom"
PlacementTarget="{Binding ElementName=CustomButton}"
StaysOpen="False">
<behaviours:Interaction.Triggers>
<behaviours:EventTrigger EventName="Closed">
<behaviours:ChangePropertyAction
PropertyName="Text"
TargetObject="{Binding ElementName=UrlTextBox}"
Value="" />
</behaviours:EventTrigger>
</behaviours:Interaction.Triggers>
<TextBlock
x:Name="UrlTextBox"
Width="100"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Green"
Foreground="White"
Text="A url" />
</Popup>
</StackPanel>
Now, if i change the child of the popup to a user control like the following:
<Popup
IsOpen="{Binding ElementName=CustomButton, Path=IsChecked}"
Placement="Bottom"
PlacementTarget="{Binding ElementName=CustomButton}"
StaysOpen="False">
<behaviours:Interaction.Triggers>
<behaviours:EventTrigger EventName="Closed">
<behaviours:ChangePropertyAction
PropertyName="Text"
TargetObject="{Binding ElementName=CustomControl, Path=UrlTextBox}"
Value="" />
</behaviours:EventTrigger>
</behaviours:Interaction.Triggers>
<local:CustomControl x:Name="CustomControl" />
</Popup>
CustomControl:
<UserControl
x:Class="WpfTests.CustomControl"
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:local="clr-namespace:WpfTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<TextBlock
x:Name="UrlTextBox"
Width="100"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Green"
Foreground="White"
Text="A url" />
The behaviour crashes, because it cannot find the property text and i don't know why.
I know i can do this using code behind, but i would like to do it in the xaml.
Any help would be appreciated
This line isn't going to work
TargetObject="{Binding ElementName=CustomControl, Path=UrlTextBox}"
Because the Path part needs to point to a Property (the TextBlock inside the UserControl is compiled as a public field).
You need to add a line in you UserControl code-behind like this:
public TextBlock UrlText => this.UrlTextBox;
and then in your behaviour change it to this
TargetObject="{Binding ElementName=CustomControl, Path=UrlText}"
Related
My code:
<Window ...
Title="Notification" Height="90" Width="300" ResizeMode="NoResize" WindowStyle="None" DataContext="{Binding Notification, Source={StaticResource Locator}}" Opacity="{Binding TransitionOpacity}" Left="{Binding LeftMargin}" Top="{Binding TopMargin}" Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibility}}">
<Window.Resources>
<local:BoolToVisibleOrHidden x:Key="BoolToVisibility" />
</Window.Resources>
<Grid Background="#FF3C4759">
...
</Window>
While compiling I get exception System.Windows.Markup.XamlParseException and after change code to
<Window ...
Title="Notification" Height="90" Width="300" ResizeMode="NoResize" WindowStyle="None" DataContext="{Binding Notification, Source={StaticResource Locator}}" Opacity="{Binding TransitionOpacity}" Left="{Binding LeftMargin}" Top="{Binding TopMargin}" >
<Window.Resources>
<local:BoolToVisibleOrHidden x:Key="BoolToVisibility" />
</Window.Resources>
<Grid Background="#FF3C4759" Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibility}}">
...
</Window>
it works but I want to set Window visibility instead of Grid.
Binding window visibility might be not a good idea in this case. If you have a notification which you want to hide after some time - just close it (Close()) instead of hiding.
If however you still want to do this - put converter into your application ( App.xaml file). Then you will be able to use it in Window.Visibility binding. As of now - window Visibility property is set before Window.Resources are initialized, so you cannot use converter created inside Window.Resources.
Alternative way is to set Visibility like this:
<Window.Resources>
<local:BoolToVisibleOrHidden x:Key="BoolToVisibility" />
</Window.Resources>
<Window.Visibility>
<Binding Path="IsVisible" Converter="{StaticResource BoolToVisibility}" />
</Window.Visibility>
Is it possible to set my StackPanel or Grid to be position absolute like CSS.
In CSS is have property Position of the elements and can set to be relative, absolute and is working good.
In XAML can make Grid, StackPanel to use position absolute.
You have to use Canvas in order to set absolute position in WPF.
In case of buttons in a window, here is a sample :
<Window x:Class="tobedeleted.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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Button Canvas.Left="10" Canvas.Bottom="20">Bottom left</Button>
</Canvas>
</Window>
The output is :
Feel free to ask if help is needed.
Absolute positioning defeats the purpose of WPF, but I agree, sometimes there is no other way so you have two basic options.
Elements under the root grid
Elements in a canvas that is the same size as the window (as Vasilievski pointed out)
Code example:
<Window x:Class="WpfApplication1.Window3"
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:WpfApplication1"
mc:Ignorable="d"
Title="Window3" Height="300" Width="300">
<Grid>
<Rectangle Fill="Red" Width="100" Height="120"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Panel.ZIndex="13"
Margin="12,34"
/>
<Rectangle Fill="Green" Width="100" Height="120"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="24,54"
/>
<Canvas>
<Rectangle Canvas.Left="5" Canvas.Top="5" Panel.ZIndex="2" Fill="Yellow" Width="120" Height="30" />
<Rectangle Canvas.Left="25" Canvas.Top="17" Panel.ZIndex="0" Fill="Blue" Width="120" Height="30" />
</Canvas>
</Grid>
</Window>
try this.
<StackPanel>
<Canvas>
<TextBlock Canvas.Left="10" Canvas.Top="6" Text="Choose a value from combobox:" Width="180"/>
<ComboBox Canvas.Left="190" Canvas.Top="4" Width="180"></ComboBox>
</Canvas>
</StackPanel>
RESULT:
How to:
Disable resizing for this usercontrol. In other words, when the user grabs the corners or the sides of this usercontrol with a mouse, I dont want the user to be able to change the size of the usercontrol?
Or if there is no way to stop resizing then how do I only allow the right side of the usercontrol dragged?
<UserControl x:Class="MyEditor.MyDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="152" d:DesignWidth="590" HorizontalContentAlignment="Right" MinWidth="{Binding ElementName=VariableType}" MinHeight="{Binding RelativeSource={RelativeSource Self}}">
<Grid Width="591" Height="147" MinWidth="{Binding ElementName=VariableTypeTextBox}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="137*" />
<ColumnDefinition Width="454*" MinWidth="250" />
</Grid.ColumnDefinitions>
<Button Content="Cancel" Height="23" Margin="0,94,7,0" Name="CancelButton" VerticalAlignment="Top" Click="CancelButton_Click" Grid.Column="1" HorizontalAlignment="Right" Width="75" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" />
<Button Content="Create" Height="23" Margin="0,94,108,0" Name="CreateButton" VerticalAlignment="Top" Click="CreateButton_Click" Grid.Column="1" HorizontalAlignment="Right" Width="75" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" />
<Label Content="Variable Name " Height="28" Margin="0,12,29,0" Name="VariableName" VerticalAlignment="Top" HorizontalAlignment="Right" Width="96" Target="{Binding}" HorizontalContentAlignment="Right" />
<TextBox Height="29" Margin="0,11,7,0" Name="VarNameTextBox" VerticalAlignment="Top" KeyDown="OnKeyDownHandler" MouseLeave="MouseLeaveHandler" LostFocus="LostFocusHandler" Grid.Column="1" HorizontalAlignment="Stretch" />
<Label Content="Variable Type" Height="28" Margin="0,0,29,73" Name="VariableType" VerticalAlignment="Bottom" HorizontalContentAlignment="Right" HorizontalAlignment="Right" Width="96" />
<TextBox Height="23" Margin="0,51,7,0" Name="VariableTypeTextBox" VerticalAlignment="Top" IsReadOnly="True" Background="Silver" Foreground="Black" Grid.Column="1" HorizontalAlignment="Stretch" Width="AUTO" />
</Grid>
You've pasted the XAML for a UserControl, but your question is asking about a Window. So, you will need to place your UserControl inside a Window that is set up to not allow resizing.
A WPF Window has a ResizeMode property, which can be one of the following:
NoResize
CanMinimize
CanResize (default)
CanResizeWithGrip
You will want NoResize.
Example:
<Window x:Class="MyEditor.Views.EditorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:views="clr-namespace:MyEditor"
mc:Ignorable="d"
ResizeMode="NoResize"
Title="Editor Window">
<views:MyDialog />
</Window>
Please see the documentation for more details.
Simply set the MinWidth/MaxWidth and MinHeight/MaxHeight properties to your required value.
For Disabling : ResizeMode="CanMinimize"
<Window x:Class="XXXXXX.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:XXXXXXX"
mc:Ignorable="d"
WindowState="Maximized"
ResizeMode="CanMinimize"
Title="Window">
Here is a simple solution:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
((Window)Parent).ResizeMode = ResizeMode.NoResize;
}
Or an attached property on the UserControl if you use the Prism Library:
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="prism:Dialog.WindowStartupLocation"
Value="CenterScreen" />
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="ShowInTaskbar" Value="False"/>
Setter Property="SizeToContent" Value="WidthAndHeight"/>
</Style>
</prism:Dialog.WindowStyle>
The question may sound a little confusing, but the problem I'm currently facing is this:
<Button x:Class="sandbox.BtnLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="this">
<Button.ToolTip>
<TextBlock Background="Yellow" Text="{Binding ElementName=this, Path=LabelText}"/>
</Button.ToolTip>
<TextBlock Background="Yellow" Text="{Binding ElementName=this, Path=LabelText}"/>
</Button>
Only the second binding works, which sets the content of the button. The first one, which I would like to use to set the contents of the tooltip of the button (via the LabelText dependency property) does not work.
Is it possible to make the first binding work?
Thanks.
Try this:
<Button x:Class="sandbox.BtnLabel"
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"
x:Name="this">
<Button.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<TextBlock Background="Yellow"
Text="{Binding LabelText}" />
</ToolTip>
</Button.ToolTip>
<TextBlock Background="Yellow"
Text="{Binding ElementName=this,
Path=LabelText}" />
</Button>
We add a ToolTip element and assign it's DataContext as it's PlacementTarget which should then reach the TextBlock
I created the UserControl which has textblock in VS2010 Express for Windows Phone and added it on the MainPage.xaml. However, I would like to set the text on the the textblock either in codebehind or .xaml file. Would anyone show or give an example or link to me. Thank in advance.
<UserControl x:Class="PhoneApp1.TitleControl"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot">
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
<TextBlock x:Name="ApplicationTitle" Style="{StaticResource appTitleStyle}"/>
<Grid Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
<TextBlock x:Name="PageTitle" Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
</Grid>
</StackPanel>
</Grid>
This is my MainPage.xaml:
<StackPanel x:Name="Titleqq" Grid.Row="0" Margin="12,17,0,28" Grid.ColumnSpan="2">
<local:TitleControl x:name="Title" />
</StackPanel>
There are two things to note when writing a usercontrol.
Write a class that defines properties in your usercontrol.
Bind it to the properties that you wish to expose or set in your windows phone application.
So your Usercontrol would Ideally have,
UserControl.xaml
UserControl.xaml.cs
You write your class in cs file. Bind it to the properties that you want to set in your application. So in this case you do something like this,
<UserControl x:Class="PhoneApp1.TitleControl"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
x:Name="parent"
>
<Grid x:Name="LayoutRoot"
DataContext="{Binding ElementName=parent}">
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
<TextBlock x:Name="ApplicationTitle" Style="{StaticResource appTitleStyle}"/>
<Grid Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
<TextBlock x:Name="PageTitle" Text="{Binding Path=Text"} Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
</Grid>
</StackPanel>
</Grid>
Now in your code behind write a class that inherits from UserControl and bind it...
Link1 and google on msdn Windows phone UserControl class.
Follow the link->