小编MTR*_*MTR的帖子

如果使用路径,则不会刷新绑定

在我的MainWindow中,我有一个ObservableCollection,它显示在每个Binding的Listbox中.

如果我更新我的Collection,修改将显示在列表中.

这有效:

public ObservableCollection<double> arr = new ObservableCollection<double>();

public MainWindow()
{
            arr.Add(1.1);
            arr.Add(2.2);
            testlist.DataContext = arr;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
   arr[0] += 1.0;
}
<ListBox Name="testlist" ItemsSource="{Binding}"></ListBox>
Run Code Online (Sandbox Code Playgroud)

这个版本不起作用:

public ObservableCollection<double> arr = new ObservableCollection<double>();

public MainWindow()
{
            arr.Add(1.1);
            arr.Add(2.2);
            testlist.DataContext = this;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
   arr[0] += 1.0;
}
<ListBox Name="testlist" ItemsSource="{Binding Path=arr}"></ListBox>
Run Code Online (Sandbox Code Playgroud)

你能告诉我为什么吗?我想把作为DataContext给出,因为在我的对话框中还有很多其他属性要显示,如果我不必为每个单独的控件设置DataContext,那将是很好的.

.net c# wpf xaml binding

2
推荐指数
1
解决办法
147
查看次数

这是内存泄漏还是正常行为?

在搜索内存泄漏时,我发现了一件奇怪的事情,我不知道它是否正常.

为了找到内存泄漏,我创建了一个带有新按钮和按钮检查的小测试应用程序:

List<WeakReference> WeakReferences = new List<WeakReference>();

private void Button_New(object sender, RoutedEventArgs e)
{
    WeakReferences.Add(new WeakReference(new ObjectUnderTest()));
    // Adding a bunch of other objects to test here
}
private void Button_Check(object sender, RoutedEventArgs e)
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();

    int AliveCounter = 0;
    foreach (var item in WeakReferences)
    {
        if (item.IsAlive)
        {
            AliveCounter++;
            Debug.WriteLine("Object " + item.Target.GetType().ToString() + " still alive!");
        }
    }
    if (AliveCounter > 0)
    {
        MessageBox.Show(AliveCounter.ToString() + " objects still alive!");
    }
    else
    {
        MessageBox.Show("No objects alive!");
    } …
Run Code Online (Sandbox Code Playgroud)

c# garbage-collection memory-leaks

2
推荐指数
1
解决办法
454
查看次数

设置usercontrol的子控件的样式

假设我有这样的用户控件:

<UserControl x:Class="StyleTest.UserControl1"
         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">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Grid.Row="0">Style please</Button>
    <Button Grid.Row="1">Style please</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我想将此控件中的所有按钮设置为background = green.但我不想影响我的程序中的其他按钮,我不想修改控件的代码.

我现在发现的是:

<Window x:Class="StyleTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:StyleTest"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="UserControlStyles" TargetType="Button">
        <Setter Property="Background" Value="green" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0">no Style please</Button>
    <loc:UserControl1 Grid.Column="1">
        <loc:UserControl1.Resources>
            <Style TargetType="Button" BasedOn="{StaticResource UserControlStyles}" />
        </loc:UserControl1.Resources>
    </loc:UserControl1>
</Grid>
Run Code Online (Sandbox Code Playgroud)

但是这意味着我必须将这个代码添加到控件的每个实例,以及一些额外的代码,如果我想设置例如TextBoxes的前景色.

我要找的是类似的东西:

        <Style TargetType="Buttons that are childs of UserControl1">
        <Setter Property="Background" Value="green" …
Run Code Online (Sandbox Code Playgroud)

wpf xaml styles

1
推荐指数
1
解决办法
4422
查看次数

不使用DependencyProperty更新绑定

我有很多现有的业务对象,其中包含许多属性和集合,我想在其中绑定用户接口.使用DependencyPropertyObservableCollections在这些对象中使​​用不是一种选择.据我所知,当我修改这些对象时,我希望有一种机制来在我执行此操作时更新所有UI控件.另外,我也不知道哪些UI控件绑定到这些对象以及哪些属性.

这是我现在尝试做的简化代码:

public class Artikel
{
   public int MyProperty {get;set;}
}

public partial class MainWindow : Window
{
    public Artikel artikel
    {
        get { return (Artikel)GetValue(artikelProperty); }
        set { SetValue(artikelProperty, value); }
    }

    public static readonly DependencyProperty artikelProperty =
        DependencyProperty.Register("artikel", typeof(Artikel), typeof(MainWindow), new UIPropertyMetadata(new Artikel()));

    public MainWindow()
    {
        InitializeComponent();
        test.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
         artikel.MyProperty += 1;
         // What can I do at this point to update all bindings?
         // …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml dependency-properties

0
推荐指数
1
解决办法
1918
查看次数

Xaml Design View找不到Background Brush

为了清理我的代码,我正在尝试将app.xaml拆分为单独的资源字典.这适用于运行时,但不适用于设计时:

剪辑在app.xaml

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/;component/Theme/Colors.xaml" />
            <ResourceDictionary Source="/;component/Theme/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>
Run Code Online (Sandbox Code Playgroud)

Colors.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <SolidColorBrush x:Key="backgroundBrush" Color="Gold"/>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

Styles.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

      <Style TargetType="StatusBar">
        <Setter Property="Background" Value="{StaticResource backgroundBrush}" />
      </Style>
    </ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

剪切了MainWindow.xaml

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="test" Width="800" Height="600" >
    <StatusBar Name="statusBar" DockPanel.Dock="Bottom">
        <StatusBarItem Content="{Binding statusMessage}" />
    </StatusBar>
Run Code Online (Sandbox Code Playgroud)

DesignView给出错误:错误8'{DependencyProperty.UnsetValue}'不是属性'Background'的有效值.C:\ Daten\DotNet\test\test\MainWindow.xaml 123

如果我将backgroundBrush直接放入app.xaml中,如下所示:

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/;component/Theme/Colors.xaml" />
            <ResourceDictionary Source="/;component/Theme/Styles.xaml" />
            <ResourceDictionary>
                <SolidColorBrush x:Key="backgroundBrush" Color="Gold"/>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>
Run Code Online (Sandbox Code Playgroud)

DesignView没有问题.

那么有没有办法告诉DesignView在哪里找到backgroundBrush,如果这个画笔放在一个单独的资源字典中?

wpf xaml designview resourcedictionary

0
推荐指数
1
解决办法
3811
查看次数