I really dont understand why i cant binding like it:
<Label Content="{Binding SomeObiect}"/>
In very often see something like it but in my programs dont work...
Why? :(
<UserControl x:Class="GAME___ala_Mario.View.Controls.SkillButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Converers="clr-namespace:GAME___ala_Mario.View.Converters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Name="btn_Skill">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20*"/>
<RowDefinition Height="5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<!--Tło-->
<Viewbox Grid.Column="0" Grid.Row="0"
Grid.ColumnSpan="2" Grid.RowSpan="2">
<!-- AND I USE BINDING LIKE IT: (always) -->
<Image Source="{Binding ElementName=btn_Skill, Path=BackgroundImageSource}"/>
</Viewbox>
</Grid>
</UserControl>
And Code-behind:
public partial class SkillButton : UserControl
{
[...]
public static readonly DependencyProperty BackgroundImageSourceProperty = DependencyProperty.Register("BackgroundImageSource", typeof(ImageSource), typeof(SkillButton));
public ImageSource BackgroundImageSource
{
get { return (ImageSource)GetValue(BackgroundImageSourceProperty); }
set { SetValue(BackgroundImageSourceProperty, value); }
}
}
No one answer my question...
but many of u give me a minus -.-
I don't know if this is in accordance with the standards but i do like it:
<Grid DataContext="{Binding ElementName=btn_Skill}">
<Viewbox>
<Image Source="{Binding BackgroundImageSource}"/>
</Viewbox>
</Grid>
That binding change DataContext for all child so I dont need rewrite it in all bingings
Related
I'll start with my code example. It's a WPF Application.
MainWindow.xaml:
<Window x:Class="DemoComboBoxProblem.MainWindow"
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:DemoComboBoxProblem"
mc:Ignorable="d"
Title="MainWindow" Height="60" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ComboBox Name="CB0" Grid.Column="0" Width="60">
<ComboBoxItem>CRLF</ComboBoxItem>
<ComboBoxItem>CR</ComboBoxItem>
<ComboBoxItem>LF</ComboBoxItem>
<ComboBoxItem>LFCR</ComboBoxItem>
</ComboBox>
<ComboBox Name="CB1" Grid.Column="1" Width="60">
<ComboBoxItem>CRLF</ComboBoxItem>
<ComboBoxItem>AA</ComboBoxItem>
<ComboBoxItem>BB</ComboBoxItem>
<ComboBoxItem>LFCR</ComboBoxItem>
</ComboBox>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace DemoComboBoxProblem
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CB0.Text = "CR";
CB1.Text = "AA";
}
}
}
I want to programatically select the item "CR" in CB0 and the item "AA" in CB1.
When I run this, CB0 displays nothing and (I assume) no item is selected in it. CB1 correctly displays and has selected "AA". I think this has something to do with "CR" being a prefix of "CRLF". I can get it to work if I change the order of the items, but I'm wondering: what is the correct way to make it work with the items in the order given?
Thanks
Try this, add SelectedValuePath = "Content" to the combobox properties.
<ComboBox Name="CB0" Grid.Column="0" Width="60" SelectedValuePath="Content">
<ComboBoxItem>CRLF</ComboBoxItem>
<ComboBoxItem>CR</ComboBoxItem>
<ComboBoxItem>LF</ComboBoxItem>
<ComboBoxItem>LFCR</ComboBoxItem>
</ComboBox>
in the code use:
CB0.SelectedValue = "CR";
CB1.SelectedValue = "AA";
I have a dialog containing 2 TextBlocks, a Progress Bar and a cancel Button.
Here is the XAML:
<Window x:Class="WpfApplication4.MainWindow"
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:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="Auto" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="txtFirst" Grid.Row="0" Margin="5" TextWrapping="Wrap">This is a really really really really long string that wraps</TextBlock>
<TextBlock x:Name="txtSecond" Grid.Row="1" Margin="5" Text="A Shorter string" TextWrapping="Wrap" MaxWidth="200"/>
<ProgressBar x:Name="prgProgress" Grid.Row="2" Margin="5" Height="20" />
<Button x:Name="btnCancel" Grid.Row="3" Margin="5" Height="25" Width="50"/>
</Grid>
</Window>
I would like the Window not to have a fixed height but auto adjust its height based on the size of its children and no more, but can’t see a way to do this. At the moment when I don’t assign anything to the Window’s height, it seems to adopt a height that is much bigger that the content.
Not sure why, or where it gets height value from? If I set Windows Height = “Auto” I get the same thing. All the heights for the RowDefinitions are set to “Auto”, which I take to mean ‘set row height to be row child height’.
You need to use SizeToContent property, check the msdn link.
Example:
<Window 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"
…
SizeToContent="WidthAndHeight">
I have Window with Frame and Page inside that frame.
That Page has one TextBox and i want to navigate that frame to another Page when someone started writing to that TextBox(similiar to google searching, when search result appears immediately on different view when you starts writing).
I am now using same DataContext for both Page objects, so they can both read the value of property searchedText, which i have binded to TextBox on each Page.
Page Search.xml:
<Page x:Class="Customer_UI.Search"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Search">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Name="searchBox" Grid.Column="0" Grid.Row="0" Margin="30" FontSize="28" Padding="10" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="5" KeyUp="TextBox_KeyUp" Text="{Binding Path=SearchedCustomer}" >
</TextBox>
</Grid>
</Page>
Search code-behind:
namespace Customer_UI
{
public partial class Search : Page
{
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
SearchExpanded searchExpanded = new SearchExpanded();
searchExpanded.DataContext = this.DataContext;
(this.DataContext as MainWindow.MainWindowContext).MainWindow.MainFrame.Navigate(searchExpanded);
searchExpanded.FocusSearchBox();
}
}
}
Page SearchExpanded.xml:
<Page x:Class="Customer_UI.SearchExpanded"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="SearchExpanded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Name="searchBox" Grid.Column="0" Grid.Row="0" FontSize="18" Padding="10" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="5" Text="{Binding Path=SearchedCustomer}" >
</TextBox>
</Grid>
</Page>
SearchExpanded code behind:
namespace Customer_UI
{
public partial class SearchExpanded : Page
{
public void FocusSearchBox()
{
MainWindow.MainWindowContext dc = DataContext as MainWindow.MainWindowContext;
// this has no output, the input from TextBox on Page that causes navigation to this page is probably still not reflected to dataContext.searchedCustomer property
Console.WriteLine(dc.SearchedCustomer);
// problem is that this time dc.SearchedCustomer has still lenght zero
searchBox.Focus();
}
}
}
Set the SelectionStart property to the length of the text.
I have written a toy WPF application with a Button and an ItemsControl. Each time you click the Button, the string "AnotherWord" gets added to the ItemsControl. Now, the ItemsControl is displayed as horizontally oriented StackPanel with a fixed width (500 pixels). This means that when you click the button a certain number of times (actually six times), the newly added string gets clipped, like this:
"AnotherWord AnotherWord AnotherWord AnotherWord AnotherWord AnotherWo"
This happens when the FontSize is 13; if you lower it to 12.7 then there's room for the sixth occurence of "AnotherWord". My question is: Is there a way to make this adjustment at runtime so that you avoid the overflow?
EDIT:
In the context of the question, the fixed width of the StackPanel is obligatory - we cannot use more than the 500 pixels we have. Another requirement that the font must never become bigger than 13.
Here is all the code I wrote:
<!-- MainWindow.xaml -->
<Window x:Class="FontSize.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"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate x:Key="labelTemplate">
<Label FontSize="13" Content="AnotherWord"></Label>
</DataTemplate>
<ItemsPanelTemplate x:Key="panelTemplate">
<StackPanel Orientation="Horizontal" Width="500" Height="50" />
</ItemsPanelTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0" ItemsSource="{Binding Path=MyStrings}" ItemTemplate="{StaticResource labelTemplate}"
ItemsPanel="{StaticResource panelTemplate}" />
<Button Grid.Row="1" Click="Button_Click"></Button>
</Grid>
</Window>
// MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace FontSize
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
MyStrings = new ObservableCollection<string>();
}
public ObservableCollection<string> MyStrings
{
get { return (ObservableCollection<string>) GetValue(MyStringsProperty); }
set { SetValue(MyStringsProperty, value); }
}
private static readonly DependencyProperty MyStringsProperty =
DependencyProperty.Register("MyStrings", typeof (ObservableCollection<string>), typeof (Window));
private void Button_Click(object sender, RoutedEventArgs e)
{
MyStrings.Add("AnotherWord");
}
}
}
Put your ItemsControl in a Viewbox and play with the following properties:
MaxWidth
MaxHeight
Stretch
StretchDirection
Edit
And remove the Width & Height property of your StackPanel.
Edit 2
Try something like that:
<!-- MainWindow.xaml -->
<Window x:Class="FontSize.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"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate x:Key="labelTemplate">
<Label FontSize="13" Content="AnotherWord"></Label>
</DataTemplate>
<ItemsPanelTemplate x:Key="panelTemplate">
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Viewbox Grid.Row="0" MaxWidth="500" Stretch="Uniform">
<ItemsControl
ItemsSource="{Binding Path=MyStrings}"
ItemTemplate="{StaticResource labelTemplate}"
ItemsPanel="{StaticResource panelTemplate}" />
</Viewbox>
<Button Grid.Row="1" Click="Button_Click"></Button>
</Grid>
</Window>
Edit 3
Change the horizontal alignment of the Viewbox so it isn't stretched to fill the grid. I've put "Center", replace by whatever you want.
...
<Viewbox
HorizontalAlignment="Center"
StretchDirection="DownOnly"
Grid.Row="0"
MaxWidth="500"
Stretch="Uniform">
...
I'm trying to replace the content of a WPF grid control by another WPF grid defined in a second XAML file in code (c#).
(simplified example)
Window1.xaml:
<Window x:Class="Demo1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Set Grid" Click="MenuItem_Click" />
</Menu>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Name="statusItem">Status</StatusBarItem>
</StatusBar>
<Grid Name="header" DockPanel.Dock="Top">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Name="txtHi" Grid.Row="0" Grid.Column="0">Hi</TextBlock>
<TextBlock Name="txtName" Grid.Row="0" Grid.Column="1">X</TextBlock>
</Grid>
<Grid Name="gridContent">
</Grid>
</DockPanel>
Windows2.xaml contains the grid that replaces gridContent
<Window x:Class="Demo1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid Name="grid2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="1" Grid.Row="1">Hello !!!</Label>
</Grid>
The MenuItem_Click event in the code behind Windows1.xaml.cs contains:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Window2 win2 = new Window2();
gridContent = win2.grid2;
setStatus();
}
private void setStatus() {
statusItem.Content = "gridContent has " + gridContent.RowDefinitions.Count + " rows and " + gridContent.ColumnDefinitions.Count + " columns.";
}
Although the statusItem say the gridContent contains 2 rows and 2 columns after a click on the menu, the window is not changed and does not contain the text Hello!!!
Any ideas what I'm doing wrong?
If there are better solutions to "embed" a grid from a second xaml file, please let me know.
Thanks,
Robbie
Replacing the value of the gridContent variable cannot have an effect on the controls tree.
You must first disconnect the grid2 from its parent and then add it to the children of gridContent, like this:
win2.Content = null;
gridContent.Children.Add(win2.grid2);
This works (I tried), but it is not the recommended way to create a Window, extract its content then place it in another window. You should use a UserControl in place of Window2 and then you can put it directly inside gridContent.