I'm noob as far XAML goes. So please bear with me if I ask stupid questions please. I will try to explain what I want so if you can, please help me. Atm I have code with alot of TextBlocks, let's say 20, and I want to change in all of them text to 0, Text = "0". atm I do all that manualy and I feel stupid. In C# I could just do
private string name = "0";
and in whole program every name will be "0"
can I do that in XAML so I wont have to do manualy every name. Same question is for Margin etc.
Thanks, and sorry if I explained poorly.
There are a couple ways to handle your request. You are probably looking for Styles.
XAML
<Window.Resources>
<Style TargetType='TextBox'>
<Setter Property='Text'
Value='Example' />
<Setter Property='Margin' Value='10,5'/>
</Style>
</Window.Resources>
<StackPanel>
<TextBox />
<TextBox />
<TextBox />
</StackPanel>
Screenshot
[Edit]
The other approach is the one offered by DevEstacion. In the resources section of XAML declare the variable value. Then use the StaticResource markup extension to apply the value to the element property.
Firstly you need to add a reference in the XAML file.
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Create the object like this
<sys:String x:Key="textboxValue">0</sys:String>
And use it in your objects within the xaml file.
<TextBox Text="{StaticResource textboxValue}" />
You can assign the vale on the textbox's Loaded event in the code behind.
Related
Say that I have the object:
<Grid x:Key"FooGrid>
<!--Content Here-->
</Grid>
And I normally reference it by something like:
<ContentPresenter Content="{StaticResource ResourceKey=FooGrid}"/>
And while this works, if I call it a second time, it removes the first use and moves it to the requested second use.
Clearly there's an issue of my method of calling the resource. What would be a better way of going about this, if I want a unique instance of the object, similar to what Style can create?
I'd use style, but as I understand it Style doesn't support defined content.
You can use the x:Shared attribute to do this, e.g.:
<Grid x:Key"FooGrid" x:Shared="False">
<!--Content Here-->
</Grid>
Excuse-me if my english is bad..
This happens,because the element is logical child of other element.
But the ContentPresenter is not a ContentControl, then he do this approach.
if you try the same thing with a ContentControl, you get a Runtime Error.
See:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter.content
Att,
Allan
I used "a little sheep" answer to add
x:Shared="False"
but it ruined structure of VisualBrush, so I added to code
VisualBrush vb = Application.Current.Resources["MyBrushName"] as VisualBrush;
VisualBrush newVB = vb.Clone();
and it justified my endeavours.
Using only .Clone() doesn't work.
I am facing a problem, I have a application with several setting's files but which have exactly the same fields.
Sample :
Profil1.settings
Profil2.settings
Profil3.settings
What I'd like to do in my setting window XAML is to dynamicaly change the binding source for each profile.
At the moment my XAML binding look like this :
SelectedValue="{Binding Source={x:Static Local:Properties.Profil1.Default}, Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
My problem is how to replace the "Profil1" by "Profil2" without remaking all my Binding one by one for each controls via my code behind ? Is that possible to use some king of reflection variable in the XAML binding source and than simply change the type of that variable with Profil1 type or Profil2 type ?
Anyone can help me about this ?
Thank in advance.
What you want to do is set the DataContext of your settings window to the appropriate profile. You can do it anyway you want, but by doing so, all your bindings would point to that object. To make the bindings work like you have now, you'd do:
<Window x:Class="MyNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:MyNamespace"
DataContext="{Binding Source={x:Static Local:Properties.Profil1.Default}}">
...
<ComboBox SelectedValue="{Binding Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
I have a custom login usercontrol that has a normal textbox and a passwordbox.
Blend refuses to cooperate with the PasswordBox saying that "DP.UnsetValue" is not valid for PasswordChar property. However the project compiles and runs fine in Blend or in VS2010. The cider designer in VS2010 doesn't seem to mind the error because it actually renders my UserControl for design time configuration.
Normally when I get one of these errors there is an InnerException with a path to the file/resource missing. That's not the case here and I'm not sure how to figure out how to fix it for when this comes up in the future.
I swapped the tags to turn the PasswordBox into a normal TextBox and it seems to be fine with that. However I need the input masking that PasswordBox provides. It's not very practical to comment out that object and finish styling my control in Blend.
Here's my Xaml:
<PasswordBox x:Name="PasswordTextbox" PasswordChar="*" Height="26" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Margin="5" RenderTransformOrigin="0.5,0.5" TabIndex="3">
<PasswordBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
</TransformGroup>
</PasswordBox.RenderTransform>
<PasswordBox.Effect>
<DropShadowEffect />
</PasswordBox.Effect>
<PasswordBox.Triggers>
<EventTrigger RoutedEvent="UIElement.GotFocus">
<BeginStoryboard Storyboard="{StaticResource StoryboardScaleUpFontIncrease}"/>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.LostFocus">
<BeginStoryboard Storyboard="{DynamicResource StoryboardScaleNormalFontNormal}"/>
</EventTrigger>
</PasswordBox.Triggers>
</PasswordBox>
Does anyone know how to debug this behavior?
I also had the same issue in xaml designer but in a sightly different way. I add the detail here in case it is helpful to someone.
I was using a 3rd party control which has their own theme defined in a dll. I referenced this dll and use 'StaticResource' to reference some resources defined in that dll. Then the xaml designer underlined my xaml code saying something like 'D.Unset is not a valid value for xxx'. But the program compile and run without any problem.
To eliminate these annoying underline, the solution is simple: Change 'StaticResource' to 'DynamicResource'.
Another more complicated way is to use 'ResourceDictionary.MergedDictionaries' to include resources in that dll into your xaml file.
I was finally able to solve this issue by using the techniques described on this blog entry:
http://web.archive.org/web/20090602111317/http://bea.stollnitz.com/blog/?p=52
I advice to recheck (twice or more) your ResourceDictionary. I came to this error after I added to my ResourceDictionary:
<Style TargetType="Label" x:Key="myLabel">
<Setter Property="Foreground" Value="{StaticResource myDefinedColor}"/>
...
</Style>
while {StaticResource myDefinedColor} was defined BELOW it. In my case error was about Foreground that has UnsetValue. So actually it really was an unset value...
I moved my style for Label below defining my color and that was it!
Just wanted to add that this was helpful in solving an issue for getting a "DependencyProperty.UnsetValue is not a valid value for property" error solved, in particular the first part of the blog suggesting that your resource might not be valid (i.e. previously defined and available for use). Turns out in my case it was the classic issue of not defining my resources in the right scope and before they are actually called upon.
My advice: First look to make sure you define the StaticResource in the appropriate Element.Resources section. Make sure that definition is physically located before your actual call to the resource.
Seems an old post. I solved my issue by putting the stuff in order of use in resourceDictionnary merging. In the example below, I called the resource 'Couleur.xaml' later and i had the error message. So I put it in first place and it solved my problem:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TS;component/Ressources/Couleur.xaml"/>
<ResourceDictionary Source="/TS;component/Ressources/ButtonStyle.xaml"/>
<ResourceDictionary Source="/TS;component/Ressources/DataGridStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
Hope it's gonna help somebody out there!
I have a very simple case that I think would benefit from using templates (but I'm not sure, which is why I'm asking). All the templating examples I've seen either assume more knowledge than I have, are too specific to be of much use to a total newb like myself, or contain lots of ancillary stuff that makes it hard to identify what's part of the template.
Here's the setup:
I have two labels side-by-side, with the first label populated with the name of a field, and the second label populated with the value of the field.
Here is the XAML I currently have in my app (many, many times):
<StackPanel Style="{StaticResource horizontalStackerStyle}">
<Label Style="{StaticResource labelStyle}">Field One:</Label>
<Label Style="{StaticResource valueStyle}" Name="field1"
Content="{Binding dataObject.field1}" />
</StackPanel>
I would like to create a template such that I could write XAML like this:
<CustomControlOrWhatever
FieldName="Field One:"
FieldValue="{Binding dataObject.field1}"/>
I have a feeling I can do this with some kind of template. One benefit of which would be that I don't need to keep specifying the styles over and over. Am I correct? How would I do this?
Thanks in advance!
UPDATE:
Still haven't found an answer to this. I chose a possible solution using Dependency Properties, and tried to ask a clarifying question here. Well, the first responder said that I don't actually need to clutter up my code behind with DP nonsense, so I changed it again--and it still doesn't work. Can anyone come up with a working solution? This seems like it should be so simple.
Just to be clear: this only needs to be one-way binding with values updated every few seconds.
What you're asking for is basically a user control.
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SomeNameSpace.SomeControlName">
<Grid x:Name="LayoutRoot">
<StackPanel Style="{StaticResource horizontalStackerStyle}">
<Label Style="{StaticResource labelStyle}" x:Name="FieldNameLbl"></Label>
<Label Style="{StaticResource valueStyle}" x:Name="ValueLbl">
</StackPanel>
</Grid>
</UserControl>
In the code behind, you'd need to expose two properties that would set the value of the controls.
public string FieldName
{
get { return FieldNameLbl.Text; }
set { FieldNameLbl.Text = value; }
}
public string FieldValue
{
get { return ValueLbl.Text; }
set { ValueLbl.Text = value; }
}
And then to call that you can put this at the top of your window/page with the rest of your declarations:
xmlns:Controls="clr-namespace:SomeNameSpace"
and then you can insert the control into your window/page like this:
<Controls:NameOfYourControl FieldName="Field One:" FieldValue="{Binding dataObject.field1}"/>
You could create a UserControl called FieldControl and define backing (automatic) properties for FieldName and FieldValue. (Normal properties would be fine, so long as you only need to bind once, which is probably the case.)
The XAML code might look like:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="foo.bar">
<StackPanel Style="{StaticResource horizontalStackerStyle}">
<Label Style="{StaticResource labelStyle}" Content="{Binding Path=FieldName, Mode=OneTime, StringFormat='{0}: '}"/>
<Label Style="{StaticResource valueStyle}" Content="{Binding Path=FieldValue, Mode=OneTime}" />
</StackPanel>
</UserControl>
Hope that helps.
What you want to do is similar to the discussion about putting images on a button with a simple way of specifying the path, like <Button MyImage="foo.jpg" />. Follow this article for the actual details.
To summarize:
One obvious way would be to create an UserControl containing your two labels and exposing the two properties. Not much templating here.
The most WPFish solution seems to be to use two Attached Properties on one of the labels (say the value), and provide a control template for it that includes the other label (the description). In the template, you bind each label text to the corresponding attached property value.
Hey, I've included an MS Access database in my WPF VB app, and I'm trying to link the data to an XCEED Datagrid. I have the following code in my testerDataSet.Designer.vb file which I assume is the funcion I should be referencing
Public ReadOnly Property Contact() As ContactDataTable
Get
Return Me.tableContact
End Get
End Property
I'm trying to get it to fill my datagirid using this
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_contacts" Source="{Binding Path=Contact, *Source={x:Static testerDataSet}*}"/>
</Grid.Resources>
<xcdg:DataGridControl Margin="54,18,4,3" Name="DataGridControl1" ItemsSource="{Binding Source={StaticResource cvs_contacts}}"/>
Unfortunately the bolded/stared part is givi ng me errors, does anyone know the correct code i should be using here to reference my source?
Thanks guys!
EDIT: Okay let me try and outline what I've done... I've added an Access 2007 Database called "tester" to my project as an existing item, and VS has gone and made testerDataSet for me, and inside testerDataset.Designer.vb I assume the first code above is the code i need to display my table data.
Basically my entire code for Window1.xaml is as follows (its just a test project to see If I can actually get the database working)
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="369" Width="503" xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid>
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_contacts" Source="{Binding Path=Contact, Source={StaticResource testerDataSet}}"/>
</Grid.Resources>
<xcdg:DataGridControl Margin="54,18,4,3" Name="DataGridControl1" ItemsSource="{Binding Source={StaticResource cvs_contacts}}"/>
</Grid>
What I'm trying to achieve is for the datagrid to display the data in the Contact datatable. I'm probably missing something important here (I'm quite new to coding =/ ) To be perfectly honest I've had a hard time finding appropriate tutorials for this so I'm not entirely sure what I'm doing
THanks again
Can you update your sample to include the code for the static resource testerDataSet?
One way you can try and work around this problem is to set the Binding directly in imperative code.
DataGridControl1.DataContext = testerDataSet.Contact
Then you could modify your WPF code to be the following
<xcdg:DataGridControl
Margin="54,18,4,3"
Name="DataGridControl1"
ItemsSource="{Binding}" />
That may not be 100% what you're looking for but it should at least temporarily unblock you.