|
Use a ViewModel to hold the calculated value. A value converter is a little bit overkill for this purpose.
|
|
|
|
|
|
I'm trying to create a single control (Ratings) with a Textblock and a Dependency property
Ratings.XAML:
<UserControl
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:TestControl"
mc:Ignorable="d"
x:Class="TestControl.Ratings"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<TextBlock x:Name="RatingText" Text="{Binding Score}" Height="60" Margin="180,157,381,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
</Grid>
</UserControl>
and Ratings.xaml.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestControl
{
public partial class Ratings : UserControl
{
public Ratings()
{
this.InitializeComponent();
}
public static readonly DependencyProperty ScoreProperty = DependencyProperty.Register( "Score", typeof(int), typeof(Ratings), new FrameworkPropertyMetadata(1));
public int Score
{
get { return (int)GetValue(ScoreProperty); }
set { SetValue(ScoreProperty, value); }
}
}
}
Then I'm trying to call it from a project changing the Dependency Parameter
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestControl"
x:Class="TestControl.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<local:Ratings Score="20" Height="120" Margin="96,51,292,0" VerticalAlignment="Top"/>
</Grid>
</Window>
and cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace TestControl
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
}
}
But it isn't working!!! Can someone please tell me why???
|
|
|
|
|
You haven't set the DataContext for the UserControl :
public Ratings()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}
NB: You'll probably want to set the DataContext on a child of the UserControl , not the UserControl itself; otherwise, you won't be able to bind the properties of the UserControl to the parent DataContext :
http://stackoverflow.com/a/13308966/124386[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
hi men
I have a listView called lw1.
When I use lw1.selectedItem, I can check which item is selected.
But, how I can know which item is alone highlighted by mouse cursor?
note:http://dl.dropbox.com/u/45898865/Immagine.jpg[^]
in picture 1
gimppoi is selected with one click of mouse cursor
in picture 2
gimppoi is selected with one click of mouse cursor
poweooooooooo has alone mouse cursor over the line
good idea
I had tried it yesterday without success.
private void buttonGV_Click(object sender, RoutedEventArgs e)
{
Attivita attivita = null;
for (int i = 0; i < listViewAttivita.Items.Count; i++ )
if ( listViewAttivita.IsMouseOver )
attivita = listViewAttivita.Items[i] as Attivita;
if ( listViewAttivita.IsMouseOver )
this test is needless, because I want know which row is selected. That test told me if the mouse is over one of all ListView rows.
Can you tell me what I should search to understand which row has the mouse over?
this is the XAML part. Does it help?
<ListView ItemsSource= "{Binding AttivitaCollection, UpdateSourceTrigger=PropertyChanged}" Height="206" HorizontalAlignment="Left" Margin="12,33,0,0" Name="listViewAttivita" VerticalAlignment="Top" Width="auto" SelectionChanged="listViewAttivita_SelectionChanged">
<ListView.Resources>
<med:ImageConverter x:Key="imageConverter"/>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Azioni registrate" Width="150" DisplayMemberBinding="{Binding nome, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<GridViewColumn Header="Stato" Width="auto" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Width="24" Name="buttonGV" Click="buttonGV_Click" Height="24" Background="Transparent" BorderBrush="Transparent">
<Image Source="{Binding Path=imgStato, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource imageConverter}}" />
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I already posted this message
http://www.codeproject.com/Messages/4441464/highlight-list-view.aspx[^]
|
|
|
|
|
You're doing it wrong .
1) call the HitTest method to get the UIElement that is under the mouse.
2) chase up the VisualTree til you get a null or a ListViewItem.
|
|
|
|
|
I'm writing a program that construct a huge diagram. Also this diagram can be save to png file. I have no problems when I render my diagram on WPF canvas. The problem start when I trying save it to file. DrawingContext.DrawText stop drawing the text with size==10 when horizontal text position becomes more than 11K pixels!
Here is the code that shows this effect.
I create simple WPF application project and create one class (MainViewControl):
Here is the code for MainViewControl.cs:
class MainViewControl : FrameworkElement
{
public void Draw(DrawingContext context)
{
for (var i = 0; i < Width / 100; i++)
{
var text = new FormattedText((i * 100).ToString(CultureInfo.InvariantCulture), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Verdana"), 10, Brushes.Blue);
var textPos = new Point((double)i * 100f + 0.789 - text.Width / 2, Height / 2 - text.Height / 2);
context.DrawLine(new Pen(Brushes.Red, 1.0), new Point(i * 100, 0), new Point(i * 100, Height));
context.DrawRectangle(Brushes.White, null, new Rect(textPos.X, textPos.Y, text.Width, text.Height));
context.DrawText(text, textPos);
}
}
protected override void OnRender(DrawingContext context)
{
Draw(context);
}
} Here is the code for MainWindow.xaml:
<Window x:Class="DrawTextProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DrawTextProblem="clr-namespace:DrawTextProblem"
Title="MainWindow" >
<DockPanel>
<Button Content="Save" Click="ButtonClick" DockPanel.Dock="Top" HorizontalAlignment="Left" />
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" DockPanel.Dock="Top">
<DrawTextProblem:MainViewControl Width="50000" Height="200" x:Name="mainViewControl" />
</ScrollViewer>
</DockPanel>
</Window>
When click the button I do this:
private void ButtonClick(object sender, RoutedEventArgs e)
{
var dlg = new Microsoft.Win32.SaveFileDialog { FileName = "Image", DefaultExt = ".png", Filter = "Images (.png)|*.png" };
if (dlg.ShowDialog() == true)
{
try
{
var bounds = new Rect(0, 0, mainViewControl.Width, mainViewControl.Height);
var diagramBitmap = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96d, 96d, PixelFormats.Default);
var drawingVisual = new DrawingVisual();
var context = drawingVisual.RenderOpen();
context.DrawRectangle(Brushes.White, null, bounds);
mainViewControl.Draw(context);
context.Close();
diagramBitmap.Render(drawingVisual);
var imageFile = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(diagramBitmap));
encoder.Save(imageFile);
imageFile.Flush();
imageFile.Close();
MessageBox.Show("Image successfully saved.", "Image saved", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception exception)
{
MessageBox.Show(string.Format("Error:\n{0}", exception.Message), string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
When I start this application I see MainViewControl on the MainWindow with all text labels every 100 pixels. If I click save button, text labels are disappearing in saved image after 11K pixels!
It's the same for text size = 10, 11 or 7, BUT not for 8, 9, 16 etc. Moreover, If I use text size 10.001 it works fine!
Does anybody know about this problem? It's a WPF bug?
|
|
|
|
|
I have the exact same issue! I would appreciate any help
|
|
|
|
|
How do you stop a WebBrowser object in WPF?
The stop method is not an option becasue that is only available in windows forms.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.stop.aspx
And Dispose does not seem to be the answer either.
I am not using the webbrowser class for its visible properties to the user. I am using it to navagate several URL's in a row. This can have a wierd effect where the browser starts sending back load completed messages when the body of the html is not usable and I just want it stopped.
|
|
|
|
|
Please do not post the same question more than once. you can edit your original if it needs more detail.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
How do you stop a WebBrowser object in WPF?
The stop method is not an option becasue that is only available in windows forms.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.stop.aspx
And Dispose does not seem to be the answer either.
|
|
|
|
|
If I want to display the word "hello" in a flowdocument I would write:
<paragraph>
<run>hello</run>
</paragraph>
How do I get each letter of the word "hello" to be a different colour?
|
|
|
|
|
Each letter will be in its own Run and you would style them individually.
|
|
|
|
|
Thank's for your reply. Run adds a space so I get "H e l l o"
|
|
|
|
|
Use a Span tag for each character:
<Span Foreground="Red">H</Span><Span Foreground="Orange">e</Span><Span Foreground="Yellow">l</Span><Span Foreground="Green">l</Span><Span Foreground="Blue">o</Span>
|
|
|
|
|
Span adds leading spaces too
|
|
|
|
|
I tested it! ... (in WPF, not Silverlight)
Of course, there can be no spaces between the Span tags (end of one Span and beginning of the next).
Just as I showed in posting above...
|
|
|
|
|
Ah - I just found that out too - it's the same with Run
Thanks for your help
|
|
|
|
|
You were right - but all the Run elements have to be on the same line.
Thanks for your help<
|
|
|
|
|
hi.
I'm trying to insert a stackpanel into a columndefinion programmatically
I do this:
two colored rectangles
stackpanel.Children.Insert(0, rectng);
stackpanel.Children.Insert(1,recText);
ViewerGrid.ColumnDefinitions[index].SetValue(ContentProperty, stackpanel);
i don't receive nothing at the grid. empty at all.
maybe the way i work with the columndefinitios is not the way that should.
thanks.
|
|
|
|
|
The column information is an attached property. I think you want something like:
stackpanel.Children.Insert(0, rectng);
stackpanel.Children.Insert(1,recText);
ViewerGrid.Children.Add(stackpanel);
Grid.SetColumn(stackpanel, index);
|
|
|
|
|
|
hi,I have a grid that background is transparent,
how i can save the image that the grid is display?
|
|
|
|
|
Hello guys,
i have a Treeview with CheckBox and TextBlock, and i wanna know how i can change the backgroundcolor for the textblock and for each parentnode in the tree when the checkbox is checked?!
I have try to set trigger and setter, but i dont have the right solution.
Can anyone please help me?
friendly regards
|
|
|
|
|
I have a window with 2 splitters. Thus makes up a left pane, a tab area, and a right pane. Think Visual Studio
The left splitter works fine, but the right does not. Here's the XAML. Paste into a window and you'll see what I mean. The spliiter is there, just all the way over to the right:
<Window x:Class="FMG.UI.WPF.Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350"
Width="525"
Closing="Window_Closing"
Background="LightSkyBlue">
<!--Title="{Binding Source={x:Static classes:FMGEngine.AppLongName}}"-->
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..\Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!--Menu-->
<Menu Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="5"
IsMainMenu="True">
<MenuItem Header="_File" />
<MenuItem Header="_Edit" />
<MenuItem Header="_View" />
<MenuItem Header="_Window" />
<MenuItem Header="_Help" />
</Menu>
<!--Toolbar-->
<ToolBar Name="tbrStandard"
Grid.Row="1"
Grid.ColumnSpan="5">
<Button>One</Button>
<Button>Two</Button>
<Button>Three</Button>
</ToolBar>
<!--Left Side Grid-->
<Grid Grid.Row="2"
Grid.Column="0"
HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Search Area Grid-->
<Grid Grid.Row="0"
Grid.Column="0"
Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"
Width="Auto"/>
<Button Grid.Column="1"
Height="24"
Width="24"/>
</Grid>
<ListBox Grid.Row="1"
Grid.Column="0"
Background="Aqua"
HorizontalAlignment="Stretch"
Width="Auto"/>
</Grid>
<!--Left Verticle Splitter-->
<GridSplitter Grid.Row="2"
Grid.Column="1"
Background="Gray"
ResizeDirection="Columns"
HorizontalAlignment="Left"
Width="10"/>
<!--Tabs-->
<TabControl Grid.Row="2"
Grid.Column="2"
Background="Salmon"/>
<!--Right Verticle Splitter-->
<GridSplitter Grid.Row="2"
Grid.Column="2"
ResizeDirection="Columns"
HorizontalAlignment="Right"
Width="10"/>
<!--Right Side-->
<Grid Grid.Row="2"
Grid.Column="4"
HorizontalAlignment="Stretch"
Background="Red">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="1"
Grid.Column="0"
Background="SteelBlue"
HorizontalAlignment="Stretch"
Width="Auto"/>
</Grid>
<!--Status Bar-->
<StatusBar Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="4"
Height="24"/>
</Grid>
</Window>
Anyone see what's wrong?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|