|
Start here[^]. You could also look at these articles[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I am trying to change the appearance of a control when the value of a property changes. I am using the XAML and C# code below, but nothing happens when I click on the button that changes the value of the Property myProperty, except when I first run the application. Any suggestions?
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Height="34" HorizontalAlignment="Left" x:Name="toggleBroadcast" VerticalAlignment="Top" Width="133" Margin="180,0,0,0">
<Button.Style>
<Style x:Name="bb" TargetType="{x:Type Button}">
<Setter Property="Content" Value="Original Content"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=myProperty}" Value="true">
<Setter Property="Content" Value="IS TRUE"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=myProperty}" Value="false">
<Setter Property="Content" Value="IS FALSE"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button Content="Click on This Button" HorizontalAlignment="Left" Width="158" Click="Button_Click_1" Height="34" VerticalAlignment="Top"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApplication3
{
public partial class MainWindow : Window
{
Boolean _Check = false;
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
public Boolean check
{
get
{
return _Check;
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_Check = !_Check;
}
}
}
|
|
|
|
|
There are a couple of things wrong with your code here. The first thing is that you don't have to supply both true and false DataTrigger elements. Add one of those triggers as the default content. When the bool value goes to the default value, the style trigger is removed.
Now, as to why you aren't seeing the value change. Even though you are changing the value, you aren't telling the XAML that the value has changed. Make this class implement INotifyPropertyChanged and have your code raise the PropertyChanged event when the vale changes. This raises the change notification.
Oh, and you aren't binding to check in your example. You are binding to myProperty.
|
|
|
|
|
First of all, you have not done any binding to your Check Property, binding to myProperty will not help you at all. You need to change your binding to bind to the Check property. Secondly for bindings to react to changes in the bound property you need to implement the INotifyPropertyChanged[^] interface in you viewmodel/code behind. Try this code :
<Grid>
<Button Height="34" HorizontalAlignment="Left" x:Name="toggleBroadcast" VerticalAlignment="Top" Width="133" Margin="180,0,0,0">
<Button.Style>
<Style x:Name="bb" TargetType="{x:Type Button}">
<Setter Property="Content" Value="Original Content" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Check}" Value="true">
<Setter Property="Content" Value="IS TRUE" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Check}" Value="false">
<Setter Property="Content" Value="IS FALSE" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button Content="Click on This Button" HorizontalAlignment="Left" Width="158" Click="Button_Click_1" Height="34" VerticalAlignment="Top" />
</Grid>
public partial class MainWindow : Window,INotifyPropertyChanged
{
Boolean _check = false;
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
public Boolean Check
{
get
{
return _check;
}
set
{
if (_check != value)
{
_check = value;
OnPropertyChanged("Check");
}
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Check = !Check;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
We have a scene designer in our app that allows the user to add elements and resize, rotate, and position them.
When the user tries to resize to make the adorned element shorter, the adorner reaches a certain size and stops getting smaller (both vertically and/or horizontally), but the adorned element continues to get smaller.
I've looked and looked, but can't find anything that resembles a minimum height or width property.
There's a LOT of code associated with this problem (the adorner class is 525 lines all by itself, and then there's the xaml for the thumbs), so I'm not real fond of the idea of posting ALL of the code at once unless it's absolutely necessary.
Can anyone provide guidance?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
There is a MinHeight and MinWidth, but it should be undefined by default. This is one of those "look around at random sh*t until you stumble across the issue" type problems . I'd start at figuring out what size the adorner stops working at. Is it the same for height and width? Is it some arbitrary number like 47.488484 or is it something system metric-y like 32? Once you have figured out the height and width that the adorner stops shrinking at, I'd rack your brain and look through the code to try to figure out where those numbers are coming from. I'd also put diagnostic messages in all the adorner layout calculation functions to see what sizes & points are coming in and what size & points you are returning and what you are doing with your visuals. Also check that the mouse messages are being processed as expected.
|
|
|
|
|
Actually, it's got something to do with RenderSize on the adorned element. I thought I had it figured out today when I overrode OnRender , and set the render size in there, but when I do something else (like drag, resize, or rotate), the RenderSize goes back to its original value, but then immediately reverts to it's set size (because I set it in OnRender ). Furthermore, the rotation code STILL uses the (wrong) render size values to set the center of the rotate transform.
I hate WPF. If you think it would be worth it, I could post my adorner code (but like I said before, there's a LOT of it).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
You usually just override the MeasureOverride and the ArrangeOverride methods. Those two methods have to return the correct sizes and arrange everything properly for the adorners to work right.
|
|
|
|
|
I have a requirement where I am using a datagrid and the number of columns will not be known until runtime. I found the following article and was able to get it working.
http://elegantcode.com/2010/03/08/silverlight-datagrid-populate-dynamic-columns-from-a-child-collection/
However this solution uses the XamlReader and creates a datatemplate in the code behind. I am not entirely happy with this solution. Is there another way that I can achieve the same result without using the XamlReader. Essentially im trying to achieve something like this
CellTemp.Append(String.Format("<TextBlock Text='{{Binding Periods[{0}].{1}}}'/>", index, propertyName)); but instead of using the XamlReader create this binding in in xaml.
modified 17-Dec-12 0:34am.
|
|
|
|
|
I'm researching control packages for WPF.
I'm considering Telerik, XCeed, and DevExpress.
As far as Telerik goes, I have used them before and I like them a lot. Good controls with excellent support.
Anyone used XCeed, or DevExpress?
Please don't suggest Infragistics. They flat out suck and I'll never use them again.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
I'll be astonished if you can get a balanced response to that question, 1 lousy experience will ruin some ones opinion of a toolset. A good experience and deep knowledge will do the opposite.
We use Telerik and are very pleased to have changed from Infragistics.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Take a look at Mindscape. I use them a lot and am really impressed by them.
|
|
|
|
|
Thanks, they look nice. And their site says their controls are geared towards MVVM, which is nice
If it's not broken, fix it until it is
|
|
|
|
|
I can't praise them enough. The way I view it is, would I use this control if I had to pay for it myself? And I did.
|
|
|
|
|
Good, thanks! +5
If it's not broken, fix it until it is
|
|
|
|
|
High praise indeed.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have a DataTemplate which, when referenced, displays a Slider Bar. The setup of the slider is dependant on data passed to the Form from the calling class. The XAML for the config looks like this.
<pre lang="xml">
<Window.Resources>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="TextBoxTemplate">
<TextBox Text="{Binding ElementName=MyWindow, Path=m_csValue}" Height="23"/>
</DataTemplate>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="SliderTemplate">
<Slider Name="sli" Value="{Binding ElementName = MyWindow, Path=sli}" ValueChanged="OnSliderValueChanged" IsSnapToTickEnabled="True" Margin="5" />
</DataTemplate>
<Style TargetType="{x:Type ContentControl}" x:Key="DisplayValues">
<Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyWindow, Path=eType}" Value="{x:Static local:eTagDisplay.Text}">
<Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=MyWindow, Path=eType}" Value="{x:Static local:eTagDisplay.Slider}">
<Setter Property="ContentTemplate" Value="{StaticResource SliderTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Width="267">
<StackPanel Margin="0,37,0,133" Height="123">
<TextBlock Height="23" Name="textBlock1" Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<ContentControl Style="{StaticResource DisplayValues}" Margin="12,12,12,158" />
</Grid>
</pre>
In my Code Behind I set up the Max, Min, Tick Frequency and Value when the Form is created.
When the Slider bar is updated i find that the slider in the code behind is not bound to the slider on display, and so I'm not updating the value of the object I wish to update.
How can I ensure that the slider created in the XAML is bound to the slider in the code behind?
Regards
Tony
|
|
|
|
|
In my project I have this xaml code for my slider
<StatusBarItem Grid.Column="5" Padding="4,0,15,0">
<StackPanel x:Name="StatusBarSlider" Orientation="Horizontal">
<TextBlock x:Name="StatusBarItem"
Margin="0,1,0,1"
Foreground="{Binding Foreground,
ElementName=navigationPane}"
Padding="6,0,6,0"
Text="{Binding Value,
ConverterParameter=%,
Converter={StaticResource PercentageConverter},
ElementName=scaleSlider}" />
<RepeatButton Margin="1,0,1,0"
CommandTarget="{Binding ElementName=scaleSlider}"
ribbon:ScreenTipService.ScreenTipHeader="Zoom Out"
Style="{StaticResource {x:Static themes:SharedResourceKeys.StatusBarEmbeddedSliderDecreaseButtonBaseStyleKey}}" />
<Slider x:Name="scaleSlider"
Width="100"
VerticalAlignment="Center"
IsSnapToTickEnabled="True"
LargeChange="0.5"
Maximum="2.0"
Minimum="0.5"
ribbon:ScreenTipService.ScreenTipHeader="Zoom"
SmallChange="0.1"
TickFrequency="0.1"
Value="1.0" />
<RepeatButton Margin="1,0,1,0"
CommandTarget="{Binding ElementName=scaleSlider}"
ribbon:ScreenTipService.ScreenTipHeader="Zoom In"
Style="{StaticResource {x:Static themes:SharedResourceKeys.StatusBarEmbeddedSliderIncreaseButtonBaseStyleKey}}" />
</StackPanel>
</StatusBarItem>
My code Behind this is
ScaleTransform scaleTransform = new ScaleTransform();
Binding scaleXBinding = new Binding("Value");
scaleXBinding.Source = scaleSlider;
Binding scaleYBinding = new Binding("Value");
scaleYBinding.Source = scaleSlider;
BindingOperations.SetBinding(scaleTransform,
ScaleTransform.ScaleXProperty,
scaleXBinding);
BindingOperations.SetBinding(scaleTransform,
ScaleTransform.ScaleYProperty,
scaleYBinding);
and since I am scaling something being displayed inside a tab I use this
newTab.Content = Contact2;
Contact2.contentContainer.LayoutTransform = scaleTransform;
This binds the slider control to the contents of my contacts. Hope this helps.
|
|
|
|
|
Hi,
Thanks for your response, I've only just managed to pick this up.
I've looked at your code sample, many thanks.
The issue that I'm seeing is that using the "Name" declared for the Slider, I cannot use it in my CodeBehind without declaring an independent Slider object - which creates and new object and doesnt bind to the declared Slider in the XAML.
As the Slider is named and delcared in a the <Windows.Resources> section, and as part of a <DataTemplate>, do I need to reference by more than just the object name, e.g. would I need to call it something like someItem.sli as opposed to just sli?
Regards
Tony
|
|
|
|
|
How does one define IOleServiceProvider in a C# WPF application?
I was told that it was automatically recogized if I downloaded and installed the visual studio SDK. Is there some extra step I am missing?
http://i67.photobucket.com/albums/h292/Athono/programming/12-9-2012.png
IOleServiceProvider is from the Namespace Microsoft.TeamFoundation...
But I cannot declare this:
http://i67.photobucket.com/albums/h292/Athono/programming/12-9-2012-2.png
|
|
|
|
|
Are you trying to use this from a Visual Studio addin? That's what the VS SDK IOleServiceProvider is present in. Here's[^] an example using it.
|
|
|
|
|
<pre>
<HierarchicalDataTemplate DataType="{x:Type local:RegionViewModel}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\Region.png" />
<TextBlock Text="{Binding RegionName}" />
</StackPanel>
</HierarchicalDataTemplate>
</pre>
Hi in the above data template whenever the type is a region, it says to use a particular icon.
But I want to chose icon based on the name of the region! Could you please let me know How I do this please?
Regards
RK
|
|
|
|
|
|
I have a combo box of lookup items. It's bound to an ObservableCollection of LookupEntity. The code-behind:
private ObservableCollection<LookupEntity> _EmployeeTypes;
public ObservableCollection<LookupEntity> EmployeeTypes
{
get { return _EmployeeTypes; }
set
{
if (_EmployeeTypes != value)
{
_EmployeeTypes = value;
RaisePropertyChanged("EmployeeTypes");
}
}
}
private LookupEntity _SelectedEmployeeType;
public LookupEntity SelectedEmployeeType
{
get { return _SelectedEmployeeType; }
set
{
if (_SelectedEmployeeType != value)
{
_SelectedEmployeeType = value;
RaisePropertyChanged("SelectedEmployeeType");
}
}
}
and
public override void RefreshView()
{
ViewHeaderText = "Employee - " + employee.FullName;
FirstName = employee.FirstName;
LastName = employee.LastName;
SelectedEmployeeType = employee.EmployeeType;
}
the XAML
<ComboBox Grid.Row="2"
Grid.Column="1"
Width="120"
HorizontalAlignment="Left"
ItemsSource="{Binding EmployeeTypes}"
SelectedItem="{Binding SelectedEmployeeType, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Caption}">
<TextBlock.ToolTip>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Caption}"
FontWeight="Bold"/>
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The combo item is not being selected. Anyone see what's wrong?
If it's not broken, fix it until it is
|
|
|
|
|
As a quick solution you can use SelectedIndex bind to an int property.
|
|
|
|
|