|
Is there anyway I can bind this DataGrid to a method in the ViewModel directly?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
I have icons defined as Paths:
<!--SCOPE ICON-->
<Geometry x:Key="data1">M98.219,48.111C97...</Geometry>
<Geometry x:Key="data2">M98.219,46.948C97...</Geometry>
<UserControl x:Key="scopeIcon">
<Path>
<Path.Data>
<GeometryGroup>
<StaticResource ResourceKey="data1"/>
<StaticResource ResourceKey="data2"/>
</GeometryGroup>
</Path.Data>
</Path>
</UserControl
What I want to do now is use them in a Button control. I have subclassed Button into ImageButton:
public class MenuButton : Button
{
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(MenuButton), new UIPropertyMetadata(null));
}
and the style
<Style TargetType="Button"
x:Key="TestButtonStyle">
<pre>
<Setter Property="Height" Value="140"/>
<Setter Property="Width" Value="195"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:MenuButton}">
<Border >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
PATH(ICON) GOES HERE <=============
<TextBlock Grid.Row="1"
Grid.Column="0"Text="{TemplateBinding Caption}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="5"
Foreground="White"
FontSize="14"
TextAlignment="Center"
TextWrapping="WrapWithOverflow"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
and it's used this way:
<controls:MenuButton Caption="{Binding Caption}"
Margin="2"
Width="100"
Style="{StaticResource TestButtonStyle}"
VerticalAlignment="Top"
Command="{Binding Path=ButtonClick}"
CommandParameter="{x:Static enums:Tabs.Oscilloscope}"/>
The question is, how in the button above to I set the path icon. It will be different for each use case. I want to set it up as part of the ControlTemplate then change it each time I add a new Menu Button.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
|
How can I handle the user clicking the icon in a Window's title bar?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
AFAIK the Title bar isn't part of the area of an application that exposes events like click. I believe you will have to create some sort of custom title bar.
I found this here on CP - WPF Custom Chrome Library[^]
|
|
|
|
|
|
|
One and a half years for your first question and this is it. Could you expand your question, just a little bit? Maybe you could identify what GAF is (other than a rather crude acronym). Then, maybe you could tell us what you are trying to achieve.
This space for rent
|
|
|
|
|
What is GAF?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I want to create some controls - a Host and a Content control. There will be one Host and many Content controls. Other developers will create Content controls and use them in the Host. For example I have a Tab control. On each tab will be a host, but each tab's Content control will be different:
MainWindow
<Window x:Class="MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:Views"
Title="Demo"
WindowStartupLocation="CenterScreen"
Height="800"
Width="1000">
<pre>
<Grid>
<TabControl Margin="5">
<TabItem Header="Tab 1">
<views:HostView HostContent="ContentControl_1.xaml"/>
</TabItem>
<TabItem Header="Tab 2">
<views:HostView HostContent="ContentControl_2.xaml"/>
</TabItem>
<TabItem Header="Tab 3">
<views:HostView HostContent="ContentControl_3.xaml"/>
</TabItem>
</TabControl>
</Grid>
I'm thinking that the Host control will have a ContentPresenter bound to the HostContent DP. The question is how to create the DP HostContent so that it resolves at design time?
Thanks
If it's not broken, fix it until it is
modified 3-May-16 11:38am.
|
|
|
|
|
I'm trying to use a Digital font I downloaded as a resource:
<Window x:Class="KClock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:KClock"
WindowStyle="None"
MouseDown="Window_MouseDown"
AllowsTransparency="True"
Background="Black"
BorderBrush="Transparent"
Height="300"
Width="500">
<pre>
<Grid>
<TextBlock Text="4:40:25 pm"
Foreground="Lime"
FontSize="34">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.FontSize" Value="36" />
<Setter Property="TextElement.FontFamily" Value="Resources/DSEG14Modern-Bold" />
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
The font is in the resource, but it's unchanged in the TextBlock. What's wrong here??
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
You need to specify the font family name, separated from the folder name with a # :
The folder and font family name are delimited by a # character. The folder reference may be absolute, or relative. For example, "Custom Fonts\#My Custom Font".
<TextBlock FontFamily="./resources/#Pericles Light">
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ok, so now I have
<TextBlock Grid.Row="2"
Text="1234"
Foreground="Lime"
FontSize="40"
FontFamily="./resources/#DSEG14Modern-Bold"/>
No change
If it's not broken, fix it until it is
|
|
|
|
|
Also tried
<TextBlock Grid.Row="2"
Text="1234"
Foreground="Lime"
FontSize="40"
FontFamily="pack://application:,,,/KClock;Component/Resources/#DSEG14Modern-Bold"/>
No change
If it's not broken, fix it until it is
|
|
|
|
|
Figured it out!
<Grid>
<TextBlock Text="12345"
FontSize="40"
FontFamily="pack://application:,,,/Folder1/#DSEG14Modern-Bold" />
<TextBlock Text="12345"
FontSize="40"
FontFamily="pack://application:,,,/Folder1/#DSEG14 Modern" />
</Grid>
If it's not broken, fix it until it is
|
|
|
|
|
[UPDATE]... Sorry formatting isnt working
OVERVIEW
I'm working on a app where I need to be able produce a "report" which involves saving a piece of a view as a PNG and displaying it in Windows Photo Viewer. So I pass in the name of the topmost element and it creates the PNG and then opens the viewer. All works fine.
Now, I need to be able to exclude specific UI elements from appearing in the report. So I've written an Attached Property called ExcludeFromReport and applied it to multiple elements in my view.
Then in my class, PrintLib, I recurse the tree looking for the DP, and if it's set, I set its Visibility to Collapsed and produce the report. This work great because I can now "turn off" specific elements from appearing in the image.
PROBLEM
However.... when the property is set set to Collapsed it hides it in the UI at runtime also. So I need to find a way to store the value of the property, turn it off, then reset it. I'm an NOT looking for the default value. The default value may or may not be the value at runtime, so it can't be used.
POSSIBLE SOLUTION
Use a dictionary to hold the DP and its value before printing, then after the print routine is done, find it in the dict and reset it to its pre-print state.
CODE
Here's my class. See last method down "SetReportProperties"
public static class PrintLib
{
public static DependencyProperty ExcludeFromReportProperty =
DependencyProperty.RegisterAttached("ExcludeFromReport",
typeof(bool),
typeof(PrintLib),
new PropertyMetadata(false));
<pre>
public static void SetExcludeFromReport(DependencyObject obj, bool value)
{
obj.SetValue(ExcludeFromReportProperty, value);
}
public static bool GetExcludeFromReport(DependencyObject obj)
{
return (bool)obj.GetValue(ExcludeFromReportProperty);
}
public static void ViewImage(string filename)
{
Process process = new Process();
process.StartInfo.FileName = "rundll32.exe";
process.StartInfo.Arguments = @"C:\WINDOWS\System32\shimgvw.dll, ImageView_Fullscreen " + filename;
process.Start();
}
public static void GenerateReportFromControl(FrameworkElement element)
{
PrepareReportElements(element);
Rect rect = VisualTreeHelper.GetDescendantBounds(element);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush brush = new VisualBrush(element);
ctx.DrawRectangle(brush, null, new Rect(rect.Size));
}
int width = (int)element.ActualWidth;
int height = (int)element.ActualHeight;
RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
string filename = @"C:\temp\ReportImage.jpg";
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
{
encoder.Save(fs);
}
ViewImage(filename);
}
private static void PrepareReportElements(FrameworkElement element, int level = 0)
{
SetReportProperties(element);
var childCount = VisualTreeHelper.GetChildrenCount(element);
for (var i = 0; i <= childCount - 1; i++)
{
var visual = (FrameworkElement)VisualTreeHelper.GetChild(element, i);
SetReportProperties(visual);
if (VisualTreeHelper.GetChildrenCount(visual) > 0)
{
PrepareReportElements(visual, i);
}
}
}
private static void SetReportProperties(FrameworkElement element)
{
var excludeFromReportIsSet = element.ReadLocalValue(ExcludeFromReportProperty) != DependencyProperty.UnsetValue;
if (excludeFromReportIsSet)
{
var exclude = (bool)element.GetValue(ExcludeFromReportProperty);
if (exclude)
{
element.Visibility = Visibility.Collapsed;
}
}
}
}
If it's not broken, fix it until it is
|
|
|
|
|
I know this may sound like a bloody silly question by why not build a new view to support the print version flip to that and after printing return to your original view?
Oh wait, why not use a proper reporting tool SSRS or something?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
1) These views are already in place. Creating a new view to support the requirement means duplicating each and every view that needs to be screen grabbed. Bad idea
2) Again, creating reports means more development.
What I've come up with now is simple... Set a few Attached Properties and voila! the view becomes an image.
If it's not broken, fix it until it is
|
|
|
|
|
How about adding an inherited DP to the root, and using a trigger against the elements you want to hide? That way, WPF will take care of setting and resetting the visibility property on the child elements for you.
public static readonly DependencyProperty IsReportModeProperty = DependencyProperty.RegisterAttached(
"IsReportMode", typeof(Boolean), typeof(PrintLib),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
public static bool GetIsReportMode(UIElement target)
{
return (bool)target.GetValue(IsReportModeProperty);
}
public static bool SetIsReportMode(UIElement target, bool value)
{
target.SetValue(IsReportModeProperty, value);
}
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<Trigger Property="ns:PrintLib.IsReportMode" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a view which populates tab controls as follows:
<TabControl x:Name="MyTabControl" ItemsSource="{Binding ViewModels}" Margin="4"
Background="#DCE8F3"
SelectedIndex="0"
BorderBrush="LightGray"
TabStripPlacement="Top">
<TabControl.Resources>
<DataTemplate DataType="{x:Type vm:SmartCardViewModel}">
<view:SmartCardView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:RfidLogFileViewModel}">
<view:LogFileView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:VideoLogFileViewModel}">
<view:LogFileView/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
The above has been simplified for clarity.
What I want is two instances of the LogFileViewModel and LogFileView pair in the tab, one for video files and one for RFID files. However, the above does not allow me to have the same model class twice, for reasons that are fairly obvious. I have got round this by deriving classes from LogFileViewModel, called VideoLogFileViewModel and RfidLogFileViewModel, which initialise the base class appropriately. The tab control obtains the view model instances from the ViewModels property of the main view model. Hence my workaround makes sure that there are no duplicate view model class instances. However, it is inelegant.
Is there some more elegant way of doing this?
I was wondering is there is some jiggery pokery I can add to the <datatemplate> to allow it to identify a specific instance of LogFileViewModel i.e. to compare a string property with a fixed value.
|
|
|
|
|
As long as you had something in there that identifies whether the template is for RFID or for video, you can use the technique I demonstrate here[^] to switch to the appropriate template. All you need is something to trigger on.
This space for rent
|
|
|
|
|
Thanks Pete
This is probably ignorance on my part but I'm not sure how that would work with my case, since I want two instances of the same template visible at the same time, or more precisely, present at the same time since you can only view one tab at any given time. I need to think about this more!
|
|
|
|
|
You're using two separate ViewModel instances - the appropriate template will be rendered in place based off the ControlTemplate.
This space for rent
|
|
|
|
|
I was developing WPF Desktop App with several features. One of them was recording video into file from webcam(usb). I used AForge.NET Framework for implementing this. Everything was fine till we try to play saved videos in HTML5 media player. Supported video codecs by the player are not supported by AForge. AForge seems not to be a live project anymore. So I tried to recompile by myself the latest source code from AForge with suggested patches (include H.264) I found on the internet. But without any luck. I guess I have to look for any other solution instead of using AForge.
I am really confused I could not find any helpful information on the internet about recording video from C# (WPF). Maybe I am so strictly oriented the way AForge is working that I am not able to find any other types of solution anymore.
After one day spent on the internet I found out:
There is no way supported directly by Microsoft for WPF application to capture video into file (h264). The only useful Microsoft SDK for this kind features is supported just by WindowsPhone or Silverlight platforms. So to implement it I still have to use third party frameworks. There is a Project iSpyConnect similar to AForge and is still living. Maybe I can look for inspiration in there
Can anybody help please, to suggest me any kind of solution? It seems nobody needs to capture webcam video from WPF App since I could find on the internet just articles mostly older than 2013 for this theme
|
|
|
|
|
The MS Expression Encoder SDK v4 can save to some 18 different presets of H264; it is not limited to Silverlight or streaming; this is because the "pro" version required for expanded H264 functionality is the only version now available and is free (including Encoder 4 itself).
(The samples appear to include elements of what you are looking for).
|
|
|
|