I created a class that inherits from Window and has got a DependencyProperty called TitlebarContent.
public FrameworkElement TitleBarContent
{
get { return (FrameworkElement)GetValue(TitleBarContentProperty); }
set { SetValue(TitleBarContentProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleBarContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleBarContentProperty =
DependencyProperty.Register("TitleBarContent", typeof(FrameworkElement), typeof(tkWindowControl), new PropertyMetadata(default(FrameworkElement)));
Inside a ResourceDictionary for Styles i add a ContentControlfor this property.
Now I'd like to use the new 'WindowObject' in another application and access the new TitlebarContentProperty. I can see the items inside my Titlebar and I'm able to move the Window, resize it and more. But I cannot bind to these items. For example I'd like to add a Helpbutton inside the Titlebar. The Button is shown, but i can't click it.
<tk:tkWindowControl.TitleBarContent>
<DockPanel Grid.Row="0" FlowDirection="LeftToRight">
<Button Content="Exit" Command="{Binding ExitApplicationCommand}" Height="60" Width="60" Background="Red"/>
</DockPanel>
</tk:tkWindowControl.TitleBarContent>
My DependencyProperty is a typeof(FrameWorkElement) because i like to add several Buttons to the Titlebar.
Is it possible to use my Bindings in this way?
Set the WindowChrome.IsHitTestVisibleInChrome attached property of the Button or the parent element to true:
<Button Content="Exit" WindowChrome.IsHitTestVisibleInChrome="True" ... />
Alternatively, you should decrease the value of the CaptionHeight property of the WindowChrome.
Related
I have a custom control (NavigationContentCtrl) for displaying various Views/ViewModels.
In the Resources of the custom control, the data template for a given ViewModel points to the corresponding View (for simplicity I have included only one VM/V pair):
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:SampleMainContentViewModel}">
<views:SampleMainContentView/>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
There is also a ContentPresenter bound to an underlying Dependency Property:
<ContentPresenter Grid.Column="1"
Content="{Binding CurrentContentViewModel,
RelativeSource={RelativeSource TemplatedParent}}"/>
In the backing Dependency Property (CurrentContentViewModel), if I instantiate a VM in the property, then the control finds the corresponding View and displays it correctly.
For example, using "SampleMainContentViewModel", this works fine:
// Using a DependencyProperty as the backing store for CurrentContentViewModel. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentContentViewModelProperty =
DependencyProperty.Register("CurrentContentViewModel", typeof(object),
typeof(NavigationContentCtrl),
new FrameworkPropertyMetadata(new SampleMainContentViewModel(),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
However, if I try to set the VM when I am using an instance of the custom control (e.g. in the MainWindow):
<Grid>
<controls:NavigationContentCtrl
CurrentContentViewModel="{x:Type vm:SampleMainContentViewModel}"
/>
</Grid>
Then all I get is the string name of the VM:
Many thanks to anyone who can help.
Thanks to #Clemens for prompting my brain-reboot.
In the instantiation of the control (e.g. in the MainWindow), the control's dependency property should be bound to property:
<Grid>
<controls:NavigationContentCtrl
CurrentContentViewModel="{Binding ContentViewModel}"
/>
</Grid>
The property, ContentViewModel, is declared and set in the underlying MainWindowViewModel:
public object ContentViewModel { get; set; } = new SampleMainContentViewModel();
Further, the Dependency property can be simplified to remove the BindsTwoWayByDefault syntax and the ContentPresenter's Content property can be set using the simpler TemplateBinding syntax.
I am wondering how to show video title on top part of the screen with media transport controls. I'm using MediaPlayerElement in UWP, target version 14393.
MediaTransportControls do not provide a Title property to show video title. However, we can implement this easily as refer to Create custom transport controls.
Since we want to add a function to the control, we need to create a new class that derives from MediaTransportControls. For a detailed tutorial, please see Create a derived control under Customize the transport controls. And for a complete sample, please refer to Media Transport Controls sample.
Here, as you want the title shows on top part of the screen and only shows when
media transport control pops up, you can add the TextBlock under the Border named "ControlPanel_ControlPanelVisibilityStates_Border" and set its VerticalAlignment to Top like the following:
<Border x:Name="ControlPanel_ControlPanelVisibilityStates_Border">
<Grid>
<TextBlock VerticalAlignment="Top" Foreground="Red" FontSize="36" Text="{TemplateBinding Title}" />
<Grid x:Name="ControlPanelGrid" ...>
</Grid>
</Border>
And in the code-behind, you can implement a dependency property for the setting of the title.
public sealed class CustomMediaTransportControls : MediaTransportControls
{
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(CustomMediaTransportControls), new PropertyMetadata(null));
public CustomMediaTransportControls()
{
this.DefaultStyleKey = typeof(CustomMediaTransportControls);
}
...
}
After this, you should be able to use your custom transport controls like:
<MediaPlayerElement Name="MainMPE" AreTransportControlsEnabled="True" Source="video.mp4">
<MediaPlayerElement.TransportControls>
<local:CustomMediaTransportControls x:Name="customMTC"
Title="This is a title">
</local:CustomMediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
I am developing UWP app and I created new user control and I want to bind it to dependency property in the control's code behind (without the datacontext).
Code Behind:
public Brush Fill
{
get { return (Brush)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
// Using a DependencyProperty as the backing store for Fill. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(MyControl), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
XAML:
...
<Grid>
<Path Fill="{Binding ????}" Stretch="Fill">
...
</Path>
</Grid>
I want that my path's fill property will bind to the property Fill from code behind (the data context should hold different data so I can't use it here)
How can I do that in UWP?
x:Bind would work perfectly on this. Note x:Bind will be looking for properties, methods & events defined in your XAML's code-behind. It's a more performant binding than ElementName.
<Path Fill="{x:Bind Fill, Mode=OneWay}" />
You should be able to use the ElementName property of a binding to circumvent the data context, just as normal WPF allows you to do.
If the property is part of the user control you'll need to assign a name via x:Name to your user control in xaml to access it
<UserControl [...] x:Name="Control"...`
Then use something like{Binding ElementName=Control, Path=Fill}, as long as Fill is a property of your user control.
i got an userControl, and i want to set the image button from the page in which userControl is included.
If i set my button image in the usercontrol in this way it works but i can't change.
<Button blablabla>
<Image Source="../../../Assets/truck-512.png"/>
</Button>
So i define a property from the codebehind
public ImageSource ButtonSource
{
get { return (ImageSource)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
// Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(ImageSource), typeof(TruckFormUC),null);
and in my userContol xaml i set my image as following
<Image x:Name="imgIcon" Source="{Binding Path=ButtonSource}"/>
than i use my userControl in my page and i try to set the image source, but it doesn't work
<UC:TruckFormUC x:Name="truckForm"
ButtonSource="../../../Assets/truck-512.png"/>
Thanks a lot
The userControl and the page are in the same folder.
Your binding is looking for ButtonSource property on the DataContext object. If you want to bind to your UserControl you should use relative binding.
<Image x:Name="imgIcon" Source="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType=UC:TruckFormUC},
Path=ButtonSource}"/>
See RelativeSource markup extension MSDN article for more info
You should also be able to use element binding.
<Image x:Name="imgIcon" Source="{Binding ElementName=truckForm, Path=ButtonSource}"/>
I'm trying to have a user control where an image is passed in from its containing element. The purpose is so that I can reuse a common set of visual elements while only changing the image. For example:
The control usage:
<DataTemplate DataType={x:Type myType}>
<local:MyControl PlotIconSource="..\Images\Scatter.png"/>
</DataTemplate>
The Image inside the control
<UserControl x:Class="MyControl">
<Image Source="{Binding PlotIconSource}"/>
</UserControl>
Finally the dependency property for PlotIconSource in the code-behind for MyControl.xaml.cs.
public ImageSource PlotIconSource
{
get { return (ImageSource)GetValue(PlotIconSourceProperty); }
set { SetValue(PlotIconSourceProperty, value); }
}
public static readonly DependencyProperty PlotIconSourceProperty =
DependencyProperty.Register(
"PlotIconSource",
typeof(ImageSource),
typeof(PlotHeader),
new UIPropertyMetadata());
I'm sure I've missed something along the way so any help would be appreciated.
You might want to bind via RelativeSource or with ElementName:
<UserControl x:Class="MyControl" Name="control">
<Image Source="{Binding PlotIconSource, ElementName=control}"/>
</UserControl>
(Do not set the DataContext, it will be invisible from the outside and mess with bindings meant for an inherited DataContext)
Looks right to me, are you getting an error message or something?