I am attempting to bind Image.Source in a DataTemplate to a System.Drawing.Image as discussed here: using XAML to bind to a System.Drawing.Image into a System.Windows.Image control
<UserControl.Resources>
<media:ImageConverter x:Key="imageConverter" />
<DataTemplate DataType="{x:Type data:GameTile}" >
<StackPanel Orientation="Vertical" Margin="5" Background="Transparent">
<Viewbox>
<TextBlock FontWeight="Bold" Text="{Binding PointValue}" TextAlignment="Center" FontSize="14" />
</Viewbox>
<Image Margin="0,5,0,0" Source="{Binding Path=Image.Image, Converter={StaticResource imageConverter}}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<loop:ListBox x:Name="listBox1"
ItemsSource="{Binding Path=GameModel.Game.GameTiles}"
ItemContainerStyle="{StaticResource GameTileContainerStyle}" Orientation="Vertical" />
</Grid>
The GameTile object has an Image (not a system.drawing.image) property that points to a Picture object which has an Image property of type System.Drawing.Image.
I am binding the ItemsSource on the ListBox to a GameTiles Collection on a Game object.
Objects
public class Game
{
public XPCollection<GameTile> GameTiles
{
get { return GetCollection<GameTile>("GameTiles"); }
}
}
public class GameTiles
{
Picture fImage;
public Picture Image
{
get { return fImage; }
set { SetPropertyValue<Picture>("Image", ref fImage, value); }
}
}
public class Picture
{
private FileData fFile;
public FileData File
{
get { return fFile; }
set
{
SetPropertyValue("File", ref fFile, value);
if (string.IsNullOrEmpty(fName))
{
fName = (value == null ? string.Empty : value.FileName);
}
fImage = null;
}
}
Image fImage;
public System.Drawing.Image Image
{
get
{
if (fImage == null)
{
try
{
MemoryStream stream = new MemoryStream();
fFile.SaveToStream(stream);
stream.Position = 0;
fImage = Image.FromStream(stream);
}
catch
{
//TODO: log exception
}
}
return fImage;
}
//set { SetPropertyValue<Image>("Image", ref fImage, value); }
}
}
The images are not showing up in the ListBoxItems, but any other property that I bind to in the DataTemplate will show up. It may be worth noting that I am using Devexpress Xpo as an ORM. Also the classes represented above do implement INotifyPropertyChanged.
Any thoughts on what I may be missing?
EDIT: Forgot to mention that I have implemented a value converter as mentioned in the post that I linked to above. However, if I put a breakpoint in the converter method, it is never called.
EDIT: Added the fFile property to the code above.
I can set an Image.Source to the GameTile.Image.Image property through c#(by converting it to BitmapImage), and have it work as expected, but I'm not sure how to accomplish that with a DataTemplate through c#. I would prefer to set the binding in XAML, but would settle for a c# workaround with a DataTemplate (or something else that would work). I am pretty confident that the issue is not with the GameTile.Image property pulling image from the database because if I manually set the source on an Image.Source in c#, the image is there. It simply isn't working in the DataTemplate.
Edit: Determined the issue to be related to properties that are not directly on the DataType that I am binding to for example with and GameTile has a (int)PointValue property, a (Picture object)Image property, and a (Prize object)Prize property.
If I bind to
<TextBlock Text="{Binding PointValue}" />
it works as expected.
But if I bind to
<TextBlock Text="{Binding Prize.Name}" />
it does not work.
And If I bind to
<Image Margin="0,5,0,0" Source="{Binding Image.BitmapImage}" />
it fails also. The following graphic shows the error that is being thrown by the binding.
BindingExpression path error: 'Name' property not found on 'object'
''XPCollection' (Hash=...)'. BindingExpression:Path=Prize.Name;
DataItem=GameTile' (HashCode=...); target element is
'TextBlock'(Name=''); target property is 'Text' (type 'String')
Thanks,
Eric
Found the solution.
Had to change this:
<TextBlock Text="{Binding Prize.Name}" />
to this:
<TextBlock Text="{Binding Prize!.Name}" />
The only difference is the exclamation point(!).
This also worked for the image property.
<Image Margin="0,5,0,0" Source="{Binding Path=Image!.Image, Converter={StaticResource imageConverter}}" />
Related
I have the following code:
public event EventHandler LoadingControlVisibilityChanged;
public Visibility LoadingControlVisibility
{
get { return _LoadingControlVisibility; }
set
{
_LoadingControlVisibility = value;
if (LoadingControlVisibilityChanged != null)
LoadingControlVisibilityChanged(this, EventArgs.Empty);
}
}
<Label x:Name="loading" Visibility="{Binding Path=LoadingControlVisibility, Mode=OneWay}" Content="No Devices Detected!" FontFamily="{DynamicResource AppFont}" HorizontalAlignment="Left" Margin="110,0,0,0" FontSize="21.333" />
The first time the binding work, but after I change the LoadingControlVisibility nothing happens, after debug I notice that the event = null. Please help me solve this problem.
my text property works with no problems:
public event EventHandler UUidChanged;
public string UUid
{
get { return _uuid; }
set
{
_uuid = value;
if (UUidChanged != null) UUidChanged(this, EventArgs.Empty);
}
}
<TextBox Text="{Binding Path=UUid, Mode=OneWay}" Margin="122.48,11.26,9,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="{DynamicResource MediumFontSize}" FontFamily="{DynamicResource AppFont}" Template="{DynamicResource TxtBoxTemplate}" Height="25" >
why is this different ?
The Binding statement will not look for the event you have defined. You must implement INotifyPropertyChanged instead.
I'm not quite certain what you're trying to accomplish. My understanding is that you are trying to bind the visibility of your label to a Property named LoadingControlVisibility that is defined in another class. If that is the case, then your path is wrong in the binding. Your binding should be as follows: Visibility="{Binding LoadingControlVisibility}"
all. I have an app that scans a picture folder and displays the images along with their names in a listbox. Each image and image name (displayed in a textblock next to the image) is stored in a horizontal stackpanel inside the listbox.
I've been trying all afternoon to find a way of displaying the image name in a textbox when the user selects it in the listbox. Sounds very simple, and I'm sure it is, but I can't seem to get it to work.
Can anyone point me in the right direction as to the best way of doing this? Thanks.
Here is my xaml if it helps:
<Grid>
<ListBox ItemsSource="{Binding AllImages}" Margin="0,0,262,0" Name="listBox1" MouseLeftButtonDown="listBox1_MouseLeftButtonDown">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Width="50" Height="50" Margin="6"/>
<TextBlock Text="{Binding Name}" Margin="6" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Height="23" HorizontalAlignment="Left" Margin="265,148,0,0" Name="textBox1" VerticalAlignment="Top" Width="198" />
</Grid>
And my code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public class MyImage
{
private ImageSource _image;
private string _name;
public MyImage(ImageSource image, string name)
{
_image = image;
_name = name;
}
public override string ToString()
{
return _name;
}
public ImageSource Image
{
get { return _image; }
}
public string Name
{
get { return _name; }
}
}
public List<MyImage> AllImages
{
get
{
List<MyImage> result = new List<MyImage>();
string filePath = #"D:\Projects\Visual Studio 2010\WpfApplication5\WpfApplication5\bin\Debug\ImageFolder";
string[] files = Directory.GetFiles(filePath);
foreach (string filename in files)
{
try
{
result.Add(
new MyImage(
new BitmapImage(
new Uri(filename)),
System.IO.Path.GetFileNameWithoutExtension(filename)));
}
catch { }
}
return result;
}
}
}
Take a look at this question.
How do I bind a Listview SelectedItem to a Textbox using the TwoWay mode?
In your case use
<TextBox Height="23"
HorizontalAlignment="Left"
Margin="265,148,0,0"
Name="textBox1"
VerticalAlignment="Top" Width="198"
Text="{Binding SelectedItem.Name, ElementName=listBox1}"/>
To retrieve the selected image from code, you have at least 3 options (I assume your images are represented by a class ImageItem)
Set IsSynchronizedWithCurrentItem to true on your ListBox, and use the following code to retrieve the selected item:
ICollectionView view = CollectionViewSource(AllImages);
ImageItem selectedImage = (ImageItem)view.CurrentItem;
Bind the SelectedItem of the ListBox to a property in your DataContext:
<ListBox .... SelectedItem="{Binding SelectedImage}">
Access the SelectedItem property directly from code-behind:
ImageItem selectedImage = (ImageItem)listBox1.SelectedItem;
Of course, if you just want to show the name in a TextBlock, you can use Russell Troywest's solution
I have a button with an image inside it. This button appears on a datagrid many times for displaying the status of the row. When the user clicks the button, it changes the state of the underlying object on the row to enabled or disabled. Here is what the button looks like:
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" HorizontalAlignment="Center">
<Image Source="{Binding Converter={StaticResource EnableDisableConverter}}" Height="25" Width="25" />
</Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
The converter correctly returns the proper image based on the status. The problem is that I've switched to an MVVM model and my code for changing the image won't work anymore. My previous looked like this:
Image img = (Image)btn.Content;
if (c.Status == Administration.Web.ObjectStatus.Enabled) {
img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/enable-icon.png", UriKind.Relative));
} else {
img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/disable-icon.png", UriKind.Relative));
}
During the command that changes the status, I've tried raising a change to the property that contains the object, but it doesn't reflect it on the UI. If I do a hard refresh of the screen, the status correctly changes. Is there a way to have rebind the image in the current situation?
Bind the source of the image to some bool Enabled property and your EnableDisableConverter can than react to that value, after each change.
XAML:
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" HorizontalAlignment="Center">
<Image Source="{Binding IsEnabled, Converter={StaticResource EnableDisableConverter}}" Height="25" Width="25" />
</Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
ViewModel:
...
public bool IsEnabled
{
get
{
return _isEnabled;
}
set
{
_isEnabled=value;
NotifyPropertyChanged("IsEnabled");
}
}
...
Converter:
public object Convert(object value, ...)
{
if((bool)value)
return uri of image1;
else
return uri of image2;
}
But I don't know what are the objects in the grid and what is the ViewModel. There may be a problem. The point is to have that IsEnabled property corectly binded to those objects in the grid.
I was wondering how I would be able to bind a text block to a variable within my C# class.
Basically I have a "cart" variable in my .cs file. Within that Cart class I have access to the different totals.
The following is what I have for binding, but it does not seem to bind to the variable...
<StackPanel
Width="Auto"
Height="Auto"
Grid.ColumnSpan="2"
Grid.Row="5"
HorizontalAlignment="Right">
<TextBlock
Name="Subtotal"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=SubTotal}">
</TextBlock>
<TextBlock
Name="Tax"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=Tax}">
</TextBlock>
<TextBlock
Name="Total"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=Total}">
</TextBlock>
</StackPanel>
What is the correct way of doing it? Thanks again for the help!
If you further want the TextBoxes to update automatically when your cart class changes, your class must implement the INotifyPropertyChanged interface:
class Cart : INotifyPropertyChanged
{
// property changed event
public event PropertyChangedEventHandler PropertyChanged;
private int _subTotal;
private int _total;
private int _tax;
private void OnPropertyChanged(String property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public int SubTotal
{
get
{
return _subTotal;
}
set
{
_subTotal = value;
OnPropertyChanged("SubTotal");
}
}
public int Total
{
get
{
return _total;
}
set
{
_total = value;
OnPropertyChanged("Total");
}
}
public int Tax
{
get
{
return _tax;
}
set
{
_tax = value;
OnPropertyChanged("Tax");
}
}
}
ElementName in binding is used to reference other controls, not variables in code behind. To reference variables in code behind, you need to assign that variable to a Control's DataContext property.
Replace every occurrence of following line of code :
<TextBlock Name="Subtotal" FontFamily="Resources/#Charlemagne Std" FontSize="20" Text="{Binding ElementName=cart, Path=SubTotal}"></TextBlock>
with :
<TextBlock Name="Subtotal" FontFamily="Resources/#Charlemagne Std" FontSize="20" Text="{Binding Path=SubTotal}"></TextBlock>
And in your Window's constructor or Load event, write following code :
this.DataContext = cart;
Two solutions..
First solution:
Put the cart as DataSource in your code behind:
DataSource = cart;
And bind to it as follows:
{Binding Path=PropertyOfCart}
Second solution:
Bind to your root control with an ElementName binding, and get the cart through a property on this control:
Name your root/parent control where cart is a propery:
<UserControl .....snip..... x:Name="Root">
Bind to it like this:
{Binding ElementName=Root, Path=Cart.PropertyOfCart}
Please note that Cart must be a property of your UserControl, and not a field
You need to set your class as the data source for your form. See also this question.
ok currently i have this piece of code:
<TabItem Style="{DynamicResource MyStyle" x:Name="TabCustomers" Padding="0,1,4,1"
Header={Binding Path=customersHeader}/>
Now i want to add an icon there so I do (by removing the header above):
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image Stretch="UniformToFill" Source="{StaticResource customers}"/>
<TextBlock x:Key="textblock" Margin="4,0,0,0"
Text="{Binding Path=customersHeader}"/>
</StackPanel>
</TabItem.Header>
So far it's ok.
I would like to generalize this using a datatemplate. I assume i have to do this in my resource dictionary:
<DataTemplate x:Key="TabItemCustomersTemplate" DataType="{x:Type TabItem}">
<StackPanel Orientation="Horizontal">
<Image Stretch="UniformToFill" Source="{StaticResource customers}"/>
<TextBlock x:Key="textblock" Margin="4,0,0,0"
Text="{Binding Path=customersHeader}"/>
</StackPanel>
</DataTemplate>
and change this in my tabitem declaration:
<TabItem ... HeaderTemplate="{StaticResource TabItemCustomersTemplate}".../>
So i run into the following issues and questions:
1) binding doesnt work, why?
2) how can i access textblock from c#?
3) how can i generalize this so i dont have to copy this over and over again for different tab items (or other controls for the matter) so that i can pass my own text and image source each time? For example you might use this to create an image button and if you have 20 buttons the code becomes messy.
Any ideas?
Thank you.
if you template the header in a
tabitem, you do not need to set the
data type of the template. the
header is a property of the tab
item, it is actually a property of
type object, you can put anything in
there.
try removing the DataType="{x:Type
TabItem}" and see if it works.
you should not need to access the
textblock from c#, you should make
do with the binding system. place a
custom object in your header. then
bind this object to your textblock
then adjust the object and it will
manipulate the textblock. getting at
an element is always hard if it is
contained in a data template. you
should not need to. if you find
yourself walking the visual tree to
find a visual element you are doing
things the hard way
you can generalise this by following
suggestion 2, using a custom object,
removing the x:Key of your data
template and setting its DataType to
be the type of your custom object.
then wherever your custom object
appears you will get it data
templated properly
Try this, This is working for me
<Window.Resources>
<!-- <BitmapImage x:Key="customers" UriSource="einstein.jpg"/>-->
<DataTemplate x:Key="TabItemCustomersTemplate">
<StackPanel Orientation="Horizontal">
<Image Stretch="UniformToFill" Source="{Binding Path=Customers}"/>
<TextBlock Margin="4,0,0,0" x:Name="txt" Text="{Binding Path=CustomersHeader}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl Name="mytabcontrol">
<TabItem x:Name="TabCustomers" Padding="0,1,4,1" Header="{Binding}" HeaderTemplate="{StaticResource TabItemCustomersTemplate}">
<Label Content="myContent" Background="Red"/>
</TabItem>
</TabControl>
</Grid>
in code behind
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
var lst = new List<People>();
lst.Add(new People() { CustomersHeader = "My Customer" });
this.DataContext = lst;
}
}
public class People
{
public string CustomersHeader { get; set; }
public BitmapImage Customers { get; set; }
}
Further you can find your textblock in code behind using this
TabPanel tabPanel = GetVisualChild<TabPanel>(mytabcontrol);
if (tabPanel != null)
{
foreach (UIElement element in tabPanel.Children)
{
TabItem tabItem = element as TabItem;
var image = FindNameFromHeaderTemplate<TextBlock>(tabItem, "txt");
}
}
public static T FindNameFromHeaderTemplate<T>(TabItem tabItem, String name) where T : UIElement
{
if (tabItem == null)
{
throw new ArgumentNullException("container");
}
if (tabItem.HeaderTemplate == null)
{
return null;
}
ContentPresenter contentPresenter = GetVisualChild<ContentPresenter>(tabItem);
if (contentPresenter == null)
{
return null;
}
T element = tabItem.HeaderTemplate.FindName(name, contentPresenter) as T;
return element;
}
public static T GetVisualChild<T>(Visual referenceVisual) where T : Visual
{
Visual child = null;
for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
{
child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
if (child != null && child.GetType() == typeof(T))
{
break;
}
else if (child != null)
{
child = GetVisualChild<T>(child);
if (child != null && child.GetType() == typeof(T))
{
break;
}
}
}
return child as T;
}