I faced one issue and I would really appreciate it if you could provide any advice for this.
Error message:
System.Windows.Data Error: 4 : Cannot find source for binding with
reference 'RelativeSource FindAncestor,
AncestorType='System.Windows.Controls.ItemsControl',
AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment;
DataItem=null; target element is 'TreeViewItem' (Name=''); target
property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
I have a treeview control (C# WPF .NET 4.0), and several items are added into this treeview by using datatemplate in xaml or manually, and both uses data binding.
When new data structure is received, I need to clean all items in treeview and regenerate new one by treeview_Control.Items.Clear() and it seems working fine from GUI point of view, but when I see output window on Visual Studio it shows several error messages as like above.
I've tried to search for a solution and tried several methods but no luck yet. Someone just recommended to just ignore this error message, but I just really want to clear this up.
If you have any idea, please help me for this.
Sorry, This is a comment but I cant fit in the comment section
Can you try add a ItemContainerStyle on the TreeView and see if it fixes the error, we had the same issue in a ListView and this was the only workaround we could find to remove the error.
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
</TreeView.ItemContainerStyle>
I had same problem.When I was removing items from treeview via ViewModel ,I was receiving same harmless error messages.
I have defined style for TreeViewItem in UserControl.Resources.
In that style are also setters for content alignments.
<Style TargetType="TreeViewItem" x:Key="myTreeViewItemStyle"
BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="HorizontalContentAlignment"
Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center"/>
...</Style>
What did help to me was addint ItemContainerStyle referenc in TreeView.
<TreeView ItemsSource="{Binding Items}" ItemContainerStyle="{StaticResource ResourceKey=myTreeViewItemStyle}">
I finally found a solution that works for me. I first added a global style to my App.xaml:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
I also had to add the following to my TreeView:
<TreeView.ItemContainerStyle>
<Style
BasedOn="{StaticResource {x:Type TreeViewItem}}"
TargetType="{x:Type TreeViewItem}">
</Style>
</TreeView.ItemContainerStyle>
For some reason, the BasedOn attribute was the missing piece in my case.
Related
I have recently started developing an UWP application.
I have defined a style in my page resources like this:
<Style TargetType="AutoSuggestBox" x:Name="AutoSuggestBoxStyle">
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontSize" Value="17"/>
<Setter Property="FontWeight" Value="SemiLight"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="BorderThickness" Value="0.5"/>
<Setter Property="PlaceholderText" Value="Type Here"/>
<Setter Property="Margin" Value="0,10,0,0"/>
</Style>
Then I am using this style in the same page like:
<AutoSuggestBox Style="{StaticResource AutoSuggestBoxStyle}"
Name="SchemeSuggestBox"
QuerySubmitted="SchemeSuggestBox_QuerySubmitted"
SuggestionChosen="SchemeSuggestBox_SuggestionChosen"
TextChanged="SchemeSuggestBox_TextChanged"/>
Doing this however crashes the app with exception:
Exception = {"No installed components were detected. (Exception from HRESULT: 0x800F1000)"}
And message:
Message = "Cannot apply a Style with TargetType 'Xamarin.Forms.Platform.UWP.FormsCustomizableTextBox' to an object of type 'Windows.UI.Xaml.Controls.TextBox'."
If I remove the style from my AutoSuggestBox (The following line) the App works as expected:
Style="{StaticResource AutoSuggestBoxStyle}"
What gives? I am not applying this style to any TextBox at all.
I have already read the Official Docs on Autosuggest box (Turns out it isn't even inherited from TextBox class It does have a property as Siva Gopal described and BugFinder hinted. Apparently I was kind of an idiot to miss it).
Relevant discussion on Xamarin forum can be seen here.
I think your style's TargetType should be: TextBox but not: AutoSuggestBox and need to apply it for TextBoxStyle on AutoSuggestBox:
Style:
<Style TargetType="TextBox" x:Name="AutoSuggestBoxStyle">
...
</Style>
Style Application:
<AutoSuggestBox TextBoxStyle={StaticResource AutoSuggestBoxStyle}>
...
</AutoSuggestBox>
I'm trying to learn Styling in WPF and encountered a funny thing:
There is a difference when I apply a style at application or (main) window level.
When I define the following resource in the App.xaml:
<Application.Resource>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontStyle" Value="Italic" />
</Style>
<Style TargetType="{x:Type GroupBox}">
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Application.Resource>
the GroupBox caption is bold and italic.
When I instead define the styling in the MainWindow.xaml:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontStyle" Value="Italic" />
</Style>
<Style TargetType="{x:Type GroupBox}">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</Window.Resources>
The GroupBox caption box is only bold and not italic.
Can anybody explain this behavior?
In picking TextBlock you have unearthed something. TextBlock is not derived from Control, and thus behaves slightly differently.
See https://stackoverflow.com/a/27065140/4258144 :
there is a curious rule in WPF implicit styles are only inherited
across template boundaries by elements which inherit from the Control
class
I guess you can add to that, "unless it is globally specified in App.xaml".
UPDATE:
Following comments, here's a look at a GroupBox visual tree, taken from Snoop.
Visual Studio is displaying the following error in its output:
System.Windows.Data Error: 40 :
BindingExpression path error:
'Text' property not found on 'object' ''PropertyAppraisalWorkflowViewModel' (HashCode=35281714)'. BindingExpression:Path=Text;
DataItem='PropertyAppraisalWorkflowViewModel' (HashCode=35281714);
target element is 'ComboBox' (Name='cmbWorkflowView');
target property is 'Text' (type 'String')
The odd part is that I do not have the property "Text" bound (or even being used) for the cmbWorkflowView control.
Here is a truncated snippet of the XAML that the error is saying has the problem:
<Grid Name="grdWorkflow">
<Grid.Resources>
<Style TargetType="ComboBox" BasedOn="{StaticResource DefaultComboBox}" />
</Grid.Resources>
<ComboBox DockPanel.Dock="Right"
Name="cmbWorkflowView"
ItemsSource="{Binding ViewOptions}"
SelectedItem="{Binding SelectedView}"
props:ComboBoxProperties.SelectionChangedCommand="{Binding SelectWorkflowView}" />
</Grid>
As you can see, the ComboBox in question isn't even using the Text field let alone binding it. What is causing the error? (See below. I solved the problem, but since I could not find anything anywhere else that had this same solution to this problem, I decided to post the solution here in case it could help someone else out.)
The key to the solution was the style. In the code, the style is separated by more XAML. I had forgotten that the ComboBox had a style associated with it. When I followed the style to its source, I found the following. (This is truncated for readability, the actual style is more involved.)
<Style x:Key="DefaultComboBox" TargetType="ComboBox">
<Setter Property="Height" Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Text" Value="{Binding Path=Text}" />
<Setter Property="Background" Value="{StaticResource ComboBoxEditableFieldBrush}" />
</Style>
The style had bound the "Text" property. Once I removed Setter for the "Text" property, the error disappeared.
<Style x:Key="DefaultComboBox" TargetType="ComboBox">
<Setter Property="Height" Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Background" Value="{StaticResource ComboBoxEditableFieldBrush}" />
</Style>
I'm not sure why I'm finding it so difficult to find information on this, but I have no clue how to change the item text size of each item inside a list view.
I don't know if I'm googling the right thing but I can't seem to find an answer to this, or really anything in general about styling elements properly.
I tried doing FontSize in the ListView but it changes nothing.
Sorry for such a mundane question but I can't find anything about this.
Thanks
You need to change listview item style i.e itemcontainerstyle like this
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontFamily" Value="Segoe Ui Light"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
or you can edit listviewitem style template also which is available on Msdn.
I am currently using the code from this blogpost in order to have my TreeView highlight those items, which are currently hovered by the mouse. This is working as intended, however now I want the TreeViewItems to notify their attached ViewModels when they are hovered / not hovered.
However I'm at a loss on how I can achieve this. The corresponding XAML code looks like the following:
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="Controls:TreeViewHelper.IsMouseDirectlyOverItem" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Stile.Triggers>
</Style>
How can I bind the property from my ViewModel, named TreeNodeModel.IsHovered to the TreeViewItem (or probably the attached dependency property IsMouseDirectlyOverItem) so that I can react on those changes from within my code?
All the examples I found via google only explained how the set the background color. Thanks in advance for your time on the probably trivial answer.
In your Style, try adding a Setter which binds IsMouseDirectlyOverItem to IsHovered, and use the OneWayToSource binding mode to push the value the right way:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Controls:TreeViewHelper.IsMouseDirectlyOverItem"
Value="{Binding Path=IsHovered, Mode=OneWayToSource}" />
<Style.Triggers>
...
</Style>
EDIT: As IsMouseDirectlyOver is read-only, and read-only DPs can't be the target of any bindings, Fredrik Hedblad's PushBinding may be a possible workaround: OneWayToSource Binding for ReadOnly Dependency Property
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="pb:PushBindingManager.StylePushBindings">
<Setter.Value>
<pb:PushBindingCollection>
<pb:PushBinding TargetDependencyProperty="Controls:TreeViewHelper.IsMouseDirectlyOverItem"
Path="IsHovered" />
</pb:PushBindingCollection>
</Setter.Value>
</Setter>
<Style.Triggers>
...
</Style>