C# 如何在WPF C中动态包含基于向量的图像资源#

C# 如何在WPF C中动态包含基于向量的图像资源#,c#,wpf,xaml,canvas,vector,C#,Wpf,Xaml,Canvas,Vector,我在运行时动态创建一个按钮,并在按钮上添加一个图像作为内容(star.png)。下面的代码工作得很好,但这里是交易。我想要一个矢量化的图像在一个像素化的图像上。我可以在XAML中实现这一点,但我不知道如何通过.cs文件中的C#动态复制这一点。这是我的工作像素化恒星图像的代码 在我的MainWindow.xaml.cs内 while (sqlite_datareader.Read()) { System.Windows.Controls.Button starBtn = new Syst

我在运行时动态创建一个按钮,并在按钮上添加一个图像作为内容(star.png)。下面的代码工作得很好,但这里是交易。我想要一个矢量化的图像在一个像素化的图像上。我可以在XAML中实现这一点,但我不知道如何通过.cs文件中的C#动态复制这一点。这是我的工作像素化恒星图像的代码

在我的MainWindow.xaml.cs内

while (sqlite_datareader.Read())
{
     System.Windows.Controls.Button starBtn = new System.Windows.Controls.Button();
     Uri resourceUri = new Uri("Images/star.png", UriKind.Relative); //HERE I WANT TO ADD THE CANVAS OF THE XAML AS IMAGE
     StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(resourceUri);
     BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
     var brush = new ImageBrush();
     brush.ImageSource = temp;
     starBtn.Width = 24;
     starBtn.Height = 24;
     starBtn.Style = (Style)FindResource("StarButtonStyle");
     starBtn.Background = brush;
     splMain.Children.Add(starBtn);
}
while(sqlite_datareader.Read())
{
    System.Windows.Controls.Button starBtn = new System.Windows.Controls.Button();
    Viewbox dynamicViewbox = (Viewbox)System.Windows.Application.Current.FindResource("StarBlue");
    dynamicViewbox.StretchDirection = StretchDirection.Both;
    dynamicViewbox.Stretch = Stretch.Fill;
    starBtn.Style = (Style)FindResource("StarButtonStyle");
    starBtn.Padding = new Thickness(0,0,0,0);
    starBtn.Margin = new Thickness(0,0,0,0);
    starBtn.Width = 24;
    starBtn.Height = 24;
    starBtn.Content = dynamicViewbox;
    splMain.Children.Add(starBtn);
}
在我的App.xaml中

    <ControlTemplate x:Key="StarBlue">
        <Viewbox Stretch="Uniform">
            <Canvas Name="Layer_1" Width="300" Height="300" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                <Canvas>
                    <Path Fill="#FF44B39B">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M150.52,15.398C75.958,15.398 15.519,75.84 15.519,150.399 15.519,224.954 75.958,285.402 150.52,285.402 225.077,285.402 285.521,224.954 285.521,150.399 285.521,75.84 225.076,15.398 150.52,15.398z M150.52,266.312C86.502,266.312 34.607,214.415 34.607,150.399 34.607,86.383 86.502,34.49 150.52,34.49 214.533,34.49 266.43,86.384 266.43,150.399 266.43,214.414 214.532,266.312 150.52,266.312z" />
                        </Path.Data>
                    </Path>
                    <Path Fill="#FF44B39B">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M254.947,150.401C254.947,208.073 208.191,254.825 150.519,254.825 92.842,254.825 46.089,208.073 46.089,150.401 46.089,92.725 92.842,45.969 150.519,45.969 208.191,45.969 254.947,92.725 254.947,150.401z" />
                        </Path.Data>
                    </Path>
                    <Path Fill="#FFFFFFFF">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M196.328,220.879L150.523,196.796 104.72,220.871 113.465,169.872 76.405,133.759 127.623,126.317 150.52,79.918 173.424,126.317 224.631,133.759 187.573,169.872z" />
                        </Path.Data>
                    </Path>
                </Canvas>
            </Canvas>
        </Viewbox>
    </ControlTemplate>
<Viewbox x:Key="StarBlue">
    <Canvas Name="Layer_1" Width="300" Height="300" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <Canvas>
            <Path Fill="#FF44B39B">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M150.52,15.398C75.958,15.398 15.519,75.84 15.519,150.399 15.519,224.954 75.958,285.402 150.52,285.402 225.077,285.402 285.521,224.954 285.521,150.399 285.521,75.84 225.076,15.398 150.52,15.398z M150.52,266.312C86.502,266.312 34.607,214.415 34.607,150.399 34.607,86.383 86.502,34.49 150.52,34.49 214.533,34.49 266.43,86.384 266.43,150.399 266.43,214.414 214.532,266.312 150.52,266.312z" />
                </Path.Data>
            </Path>
            <Path Fill="#FF44B39B">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M254.947,150.401C254.947,208.073 208.191,254.825 150.519,254.825 92.842,254.825 46.089,208.073 46.089,150.401 46.089,92.725 92.842,45.969 150.519,45.969 208.191,45.969 254.947,92.725 254.947,150.401z" />
                </Path.Data>
            </Path>
            <Path Fill="#FFFFFFFF">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M196.328,220.879L150.523,196.796 104.72,220.871 113.465,169.872 76.405,133.759 127.623,126.317 150.52,79.918 173.424,126.317 224.631,133.759 187.573,169.872z" />
                </Path.Data>
            </Path>
        </Canvas>
    </Canvas>
</Viewbox>

在我的主窗口内.xaml

    <Button x:Name="unBtn" Style="{StaticResource MinButtonStyle}" Height="30" Margin="448,256,10,0" VerticalAlignment="Top" Click="unBtn_Click" FontSize="10" BorderBrush="{x:Null}">
        <StackPanel Orientation="Horizontal">
              <ContentControl Template="{StaticResource StarBlue}" />
        </StackPanel>
    </Button>


因此,确切的问题是,如何在.cs文件中动态地使用按钮上的{StarBlue}画布图形作为内容,就像while()循环中的一样?

由于Clemens的提示,我想出了一个解决方案。我使用了一个Viewbox而不是ControlTemplate

在我的MainWindow.xaml.cs内

while (sqlite_datareader.Read())
{
     System.Windows.Controls.Button starBtn = new System.Windows.Controls.Button();
     Uri resourceUri = new Uri("Images/star.png", UriKind.Relative); //HERE I WANT TO ADD THE CANVAS OF THE XAML AS IMAGE
     StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(resourceUri);
     BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
     var brush = new ImageBrush();
     brush.ImageSource = temp;
     starBtn.Width = 24;
     starBtn.Height = 24;
     starBtn.Style = (Style)FindResource("StarButtonStyle");
     starBtn.Background = brush;
     splMain.Children.Add(starBtn);
}
while(sqlite_datareader.Read())
{
    System.Windows.Controls.Button starBtn = new System.Windows.Controls.Button();
    Viewbox dynamicViewbox = (Viewbox)System.Windows.Application.Current.FindResource("StarBlue");
    dynamicViewbox.StretchDirection = StretchDirection.Both;
    dynamicViewbox.Stretch = Stretch.Fill;
    starBtn.Style = (Style)FindResource("StarButtonStyle");
    starBtn.Padding = new Thickness(0,0,0,0);
    starBtn.Margin = new Thickness(0,0,0,0);
    starBtn.Width = 24;
    starBtn.Height = 24;
    starBtn.Content = dynamicViewbox;
    splMain.Children.Add(starBtn);
}
在我的App.xaml中

    <ControlTemplate x:Key="StarBlue">
        <Viewbox Stretch="Uniform">
            <Canvas Name="Layer_1" Width="300" Height="300" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                <Canvas>
                    <Path Fill="#FF44B39B">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M150.52,15.398C75.958,15.398 15.519,75.84 15.519,150.399 15.519,224.954 75.958,285.402 150.52,285.402 225.077,285.402 285.521,224.954 285.521,150.399 285.521,75.84 225.076,15.398 150.52,15.398z M150.52,266.312C86.502,266.312 34.607,214.415 34.607,150.399 34.607,86.383 86.502,34.49 150.52,34.49 214.533,34.49 266.43,86.384 266.43,150.399 266.43,214.414 214.532,266.312 150.52,266.312z" />
                        </Path.Data>
                    </Path>
                    <Path Fill="#FF44B39B">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M254.947,150.401C254.947,208.073 208.191,254.825 150.519,254.825 92.842,254.825 46.089,208.073 46.089,150.401 46.089,92.725 92.842,45.969 150.519,45.969 208.191,45.969 254.947,92.725 254.947,150.401z" />
                        </Path.Data>
                    </Path>
                    <Path Fill="#FFFFFFFF">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M196.328,220.879L150.523,196.796 104.72,220.871 113.465,169.872 76.405,133.759 127.623,126.317 150.52,79.918 173.424,126.317 224.631,133.759 187.573,169.872z" />
                        </Path.Data>
                    </Path>
                </Canvas>
            </Canvas>
        </Viewbox>
    </ControlTemplate>
<Viewbox x:Key="StarBlue">
    <Canvas Name="Layer_1" Width="300" Height="300" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <Canvas>
            <Path Fill="#FF44B39B">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M150.52,15.398C75.958,15.398 15.519,75.84 15.519,150.399 15.519,224.954 75.958,285.402 150.52,285.402 225.077,285.402 285.521,224.954 285.521,150.399 285.521,75.84 225.076,15.398 150.52,15.398z M150.52,266.312C86.502,266.312 34.607,214.415 34.607,150.399 34.607,86.383 86.502,34.49 150.52,34.49 214.533,34.49 266.43,86.384 266.43,150.399 266.43,214.414 214.532,266.312 150.52,266.312z" />
                </Path.Data>
            </Path>
            <Path Fill="#FF44B39B">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M254.947,150.401C254.947,208.073 208.191,254.825 150.519,254.825 92.842,254.825 46.089,208.073 46.089,150.401 46.089,92.725 92.842,45.969 150.519,45.969 208.191,45.969 254.947,92.725 254.947,150.401z" />
                </Path.Data>
            </Path>
            <Path Fill="#FFFFFFFF">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M196.328,220.879L150.523,196.796 104.72,220.871 113.465,169.872 76.405,133.759 127.623,126.317 150.52,79.918 173.424,126.317 224.631,133.759 187.573,169.872z" />
                </Path.Data>
            </Path>
        </Canvas>
    </Canvas>
</Viewbox>


您可以将Viewbox设置为资源,而不是ControlTemplate,然后通过FindResource获取它并将其分配给按钮的内容属性。但你不应该这样做。而是为按钮创建一个视图模型类,并使用ItemsControl显示按钮的集合。从这里开始阅读:。谢谢克莱门斯的提示。我已经发布了第一种方法作为答案。目前我正忙着,但我会仔细研究一下您稍后提到的第二种方法。