我试图将属性(Button.Background)绑定到我的自定义附加属性上的属性.
在我的C#文件中
public static class Square
{
public static readonly DependencyProperty PlayerProperty =
DependencyProperty.RegisterAttached
(
name : "Player",
propertyType : typeof(Player),
ownerType : typeof(UIElement),
defaultMetadata: new FrameworkPropertyMetadata(null)
);
public static Player GetPlayer(UIElement element)
{
return (Player)element.GetValue(PlayerProperty);
}
public static void SetPlayer(UIElement element, Player player)
{
element.SetValue(PlayerProperty, player);
}
// Other attached properties
}
Run Code Online (Sandbox Code Playgroud)
我的XAML片段是
<Grid Name="board" Grid.Row="1" Grid.Column="1">
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="20" />
<Setter Property="Width" Value="20" />
<Setter Property="BorderThickness" Value="3" />
<Setter Property="Background"
Value="{Binding Path=(l:Square.Player).Brush, Mode=OneWay}" /> …
Run Code Online (Sandbox Code Playgroud) 我在下面的一些简单代码中使用了ToggleButton.IsChecked属性来设置TextBlock的可见性.它工作正常.由于这不适合我的程序结构,我试图将另一个TextBlock的可见性绑定到"this"的DependencyProperty.编译很好,但没有效果.我做错了什么,只是不确定是什么.
XAML
<Window x:Class="ToggleButtonTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="200" Height="100">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<StackPanel>
<ToggleButton x:Name="toggleButton" Content="Toggle"
IsChecked="True" Checked="toggleButton_Checked"/>
<TextBlock Text="Some Text"
Visibility="{Binding IsChecked,
ElementName=toggleButton,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBlock Text="More Text"
Visibility="{Binding ShowMoreText,
ElementName=this,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
C#
using System.Windows;
namespace ToggleButtonTest
{
public partial class MainWindow : Window
{
static MainWindow()
{
FrameworkPropertyMetadata meta =
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
ShowMoreTextProperty =
DependencyProperty.Register("ShowMoreText",
typeof(bool), typeof(MainWindow), meta);
}
public MainWindow()
{
InitializeComponent();
}
public static readonly DependencyProperty ShowMoreTextProperty;
public bool ShowMoreText
{ …
Run Code Online (Sandbox Code Playgroud) 抱歉我的英语不好...RichTextBox
内容的默认值是从其RichTextBox
自身继承前景色。这很好,但如果我Foreground
为我的文本的某些部分设置了特定的颜色,那部分显然不再继承Foreground
。如何让我的“彩色”文本Foreground
再次继承?我正在尝试执行 Office Word 中的“自动”颜色之类的操作,但是在将特定颜色设置为 a 后TextRange
,我不知道如何取消设置:/
TextRange.ClearAllProperties()
做我需要的,但也会删除其他属性,比如FontSize
和FontFamily
......
TextRange.ApplyPropertyValue(ForegroundProperty, DependencyProperty.UnsetValue)
也没有做的伎俩......
我在从标准属性更新依赖项属性时遇到一些问题。
如果我将该属性设置为null或使用新数据更新它,我会以为它将重置依赖项属性。但是,看起来它只是将新数据堆积在最上面。
这是我正在使用的几个属性/依赖属性:
依赖属性
public static readonly DependencyProperty DataTableChartProperty = DependencyProperty.Register
("DataTableChart", typeof(DataTable), typeof(MainWindowViewModel));
public static readonly DependencyProperty ContentElementProperty = DependencyProperty.Register
("ContentElement", typeof(FrameworkElement), typeof(MainWindowViewModel));
Run Code Online (Sandbox Code Playgroud)
标准属性
public DataTable DataTableChart
{
get { return (DataTable)this.GetValue(DataTableChartProperty); }
set { this.SetValue(DataTableChartProperty, value); }
public FrameworkElement ContentElement
{
get { return (FrameworkElement)this.GetValue(ContentElementProperty); }
set { this.SetValue(ContentElementProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何建议。提前致谢!
这就是我现在设置的方式...用于测试...
void _bw_DoWork(object sender, DoWorkEventArgs e)
{
var loadLog = new LoadLog();
e.Result = loadLog.LoadCaseLogs(SelectedFiles);
}
void _bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataTableChart = null; …
Run Code Online (Sandbox Code Playgroud) 这里有一个简单的WPF程序:
<!-- Updater.xaml -->
<Window x:Class="Update.Updater"
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}}">
<Grid>
<StackPanel>
<Button Click="Button_Click" Height="50"></Button>
<Label Content="{Binding Label1Text}" Height="50"></Label>
<Label Content="{Binding Label2Text}" Height="50"></Label>
</StackPanel>
</Grid>
</Window>
// Updater.xaml.cs
using System.Threading;
using System.Windows;
namespace Update
{
public partial class Updater : Window
{
public Updater()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Label1Text = "It is coming...";
Thread.Sleep(3000);
Label2Text = "It is here!";
}
public string Label1Text
{
get { return (string)GetValue(CategoryProperty); }
set …
Run Code Online (Sandbox Code Playgroud) 我有以下情况:
查询:
我向UserControl添加了一个名为"SelectedCustomerItems"的依赖属性,我希望它返回一个List <CustomerModel>(仅用于IsSelected = true)
此UserControl放在另一个窗口上
但我没有通过此依赖项属性获取SelectedCustomer项.断点没有打到Get {}请建议....
我一直试图在WPF中设置这个障碍,我想我找到了一个解决方案,虽然是一个丑陋的解决方案.
方案如下:
码...
父视图
<UserControl DataContext="{Binding Source={StaticResource Locator}, Path=ParentControlLocator}">
<my:Child Demo="{Binding Path=DataContext.DemoTextAlpha, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}}" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
父类视图模型
public class ParentClassViewModel : BaseViewModel
{
private string _demoTextAlpha = "Some Alpha text";
public string DemoTextAlpha
{
get
{
return this._demoTextAlpha;
}
set
{
this._demoTextAlpha = value;
this.NotifyPropertyChange("DemoTextAlpha");
}
}
}
Run Code Online (Sandbox Code Playgroud)
儿童观
<UserControl DataContext="{Binding Source={StaticResource Locator}, Path=ChildControlLocator}">
<TextBlock Text="{Binding Path=SomeProperty}" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
儿童观点代码背后
public partial class Child : UserControl
{
public Child()
{
InitializeComponent();
}
public static …
Run Code Online (Sandbox Code Playgroud) 我试图解决一些经常发生的数据绑定错误,如下所示
System.Windows.Data Error: 40 : BindingExpression path error: 'Active' property not found on 'object' ''FrameViewModel' (HashCode=55649279)'. BindingExpression:Path=Active; DataItem='FrameViewModel' (HashCode=55649279); target element is 'ContentControl' (Name='HeaderBorder'); target property is 'NoTarget' (type 'Object')
Run Code Online (Sandbox Code Playgroud)
我有一个附加的依赖属性Active
public bool Active
{
get
{
return (bool)GetValue(ActiveProperty);
}
set
{
SetValue(ActiveProperty, value);
}
}
public static readonly DependencyProperty ActiveProperty
= DependencyProperty.RegisterAttached("Active", typeof(bool), typeof(Card));
Run Code Online (Sandbox Code Playgroud)
绑定在控件模板中的主题文件中设置
<ControlTemplate x:Key="FrameHeader" TargetType="ContentControl">
.....
.....
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Active}" Value="True">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource SelectedHeaderBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Active}" Value="False">
<Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource …
Run Code Online (Sandbox Code Playgroud) 我的页面xaml:
<header:DefaultText x:Name="header" HeaderText="{Binding Resources.HeaderTitle}"/>
Run Code Online (Sandbox Code Playgroud)
我的DefaultText.cs DependencyProperty:
public string HeaderText
{
get { return (string)GetValue(HeaderTextProperty); }
set
{ //This part is never called when binding, but it is with static text
SetValue(HeaderTextProperty, value);
SetText(value);
}
}
public readonly DependencyProperty HeaderTextProperty = DependencyProperty.Register("HeaderText", typeof(string), typeof(DefaultText), new PropertyMetadata(string.Empty));
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我使用绑定设置HeaderText属性时,不会调用setter,但是当我使用没有绑定的普通字符串时,会调用它.
我已经尝试过类似问题的答案,例如:WPF Binding to variable/DependencyProperty
所以,我有一个listview,每当创建一个项目滚动到该项目(底部)时我都想要它.因为我正在使用MVVM,所以我找到了关于如何创建一个从listview继承的新控件的非常好的解释.问题是这个答案(第三个)是指6年前的WPF.我正在制作一个UWP应用程序,因此我复制了代码并尝试将其格式化为我的需求.下面的代码不会给出任何错误或异常,而是加载"ChatListView",因为我称之为完美,然后什么都不做.与原始代码相比,注释仅进行了一些编辑.
我能做什么 ?先感谢您!
public class ChatListView : ListView
{
//Define the AutoScroll property. If enabled, causes the ListBox to scroll to
//the last item whenever a new item is added.
public static readonly DependencyProperty AutoScrollProperty =
DependencyProperty.Register(
"AutoScroll",
typeof(Boolean),
typeof(ChatListView),
new PropertyMetadata(
true, //Default value.
new PropertyChangedCallback(AutoScroll_PropertyChanged)));
//Gets or sets whether or not the list should scroll to the last item
//when a new item is added.
public bool AutoScroll
{
get { return (bool)GetValue(AutoScrollProperty); }
set { …
Run Code Online (Sandbox Code Playgroud) dependency-properties custom-controls observablecollection mvvm uwp
wpf ×8
c# ×6
binding ×3
mvvm ×3
data-binding ×1
delay ×1
richtextbox ×1
uwp ×1
wpf-controls ×1
xaml ×1