2017-11-06 2 views
1

XAML을 처음 사용하고 프론트 엔드 시스템의 UI를 수정하는 데 사용해야합니다. 필드는 고정되어 있으며 수정할 수 없으므로 솔직히 말해서 최선의 노력에도 불구하고 알아낼 수있는 기술이 없습니다.XAML Multibinding StringFormat의 기본값 설정

UI는 데이터베이스에 비디오 게임의 속성을 표시하기위한 것입니다. 특히 현재 비디오 게임 컨트롤러의 이미지를 표시 할 수있는 UI 부분에 관심이 있습니다 (다른 게임은 다른 컨트롤러를 사용함).

같이되면, UI 만 아래 코드 있듯이 플랫폼에 속하는 모든 비디오 게임을하는 제어기를 디스플레이 가능 : 위 예에서

<TextBlock x:Name="PlatformControlPanel" Visibility="Collapsed"> 
    <TextBlock.Text> 
     <MultiBinding StringFormat="{}pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{0}/{0}.png"> 
      <Binding Path="SelectedGame.Platform" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 
<Image Source="{Binding Text, ElementName=PlatformControlPanel}" RenderOptions.BitmapScalingMode="HighQuality" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform"/> 

컨트롤러 화상은 다음의 디렉토리에 저장된 형식 :/테마/사용자 정의/이미지/컨트롤/"플랫폼 이름"/ "플랫폼 이름".png

그리고 이것은 잘 작동하지만 문제는 주어진 플랫폼에 조이스틱 컨트롤러와 같은 컨트롤러 유형이 두 개 이상있을 수 있다는 것입니다. 또는 GamePad 컨트롤러.

그래서 내가 원하는 것은 주어진 플랫폼에 대한 기본 컨트롤러를 표시하지만 다른 컨트롤러 유형을 사용하는 특정 게임에 대해 다른 컨트롤러를 표시하도록 코드를 변경하는 것입니다. 나는 데이터베이스에 새로운 필드를 설정할 수 있기 때문에

가, 이렇게하려면, 내가 게임의 이름을 기반으로 컨트롤러를 표시하는 코드를 수정 :이 시점에서

<TextBlock x:Name="PlatformControlPanel" Visibility="Collapsed"> 
    <TextBlock.Text> 
     <MultiBinding StringFormat="{}pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{1}/{0}.png"> 
       <Binding Path="SelectedGame.Title" /> 
       <Binding Path="SelectedGame.Platform" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 
<Image Source="{Binding Text, ElementName=PlatformControlPanel}" RenderOptions.BitmapScalingMode="HighQuality" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform"/> 

의 UI는 /에 보이는 테마/사용자/이미지/컨트롤/"플랫폼 이름"/ "게임 제목".png

"게임 제목".png가 있으면 UI가 이미지 만 표시합니다. 그렇지 않으면 아무것도 표시되지 않습니다.

기본값을 설정하는 방법을 모르겠으므로 "플랫폼 이름"/ "게임 제목".png가 없으면 UI는 "플랫폼 이름"/ "플랫폼 이름".png로 대체됩니다. .

나를 도와 줄 사람이 있습니까?

편집 : XAML 업데이트 (관련이없는 코드는 생략보고 오류가 구축 없음) :

<UserControl xmlns:converter="clr-namespace:BigBoxTheme.Views.Converters" 

<!--Main Content Row--> 
     <Grid Grid.Row="2"> 
      <Grid.Resources> 
       <converter:MultiValueConverter x:Key="MultiValueConverter"/> 
      </Grid.Resources> 

<Image RenderOptions.BitmapScalingMode="HighQuality" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform"> 
        <Image.Source> 
         <MultiBinding Converter="{StaticResource MultiValueConverter}"> 
          <Binding Path="selectedExample"/> 
          <Binding Path="ValidationTest"/> 
         </MultiBinding> 
        </Image.Source> 
       </Image> 

C# 코드 : 그래서처럼 바인딩을 사용 TargetNullValue에서

using System; 
using System.Collections.Generic; 
using System.Globalization; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Data; 
using System.Windows.Media.Imaging; 

namespace BigBoxTheme.Views.Converters 
{ 
class MultiValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     BitmapImage result = null; 
     //process the variables passed in 
     Uri SourceUri = new Uri($"pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{values[1]}/{values[0]}.png", UriKind.Absolute);//use either string format or whatever you like to create a valid Uri 
     if (File.Exists(SourceUri.AbsolutePath)) 
     { 
      //we have found the image 
      //no need to do anything just let it run through 
     } 
     else 
     { 
      //use the default 
      SourceUri = new Uri($"pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{values[1]}/{values[1]}.png", UriKind.Absolute); 
     } 
     return new BitmapImage(SourceUri); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
} 

답변

1

:

<Binding Path="SelectedGame.Title" TargetNullValue="Platform Name" /> 

EDIT
의견을 말하고 나서 더 정확하게는 Multi Value Converter이되도록 변환기를 사용하는 것이 좋습니다.그것은 다음과 같을 것이다 :

class MultiValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     BitmapImage result = null; 
     //process the variables passed in 
     Uri SourceUri = new Uri($"pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{values[1]}/{values[0]}.png", UriKind.Absolute);//use either string format or whatever you like to create a valid Uri 
     if (File.Exists(SourceUri.AbsolutePath)) 
     { 
      //we have found the image 
      //no need to do anything just let it run through 
     } 
     else 
     { 
      //use the default 
      SourceUri = new Uri($"pack://siteoforigin:,,,/Themes/Custom/Images/Controls/{values[1]}/{values[1]}.png", UriKind.Absolute); 
     } 
     return new BitmapImage(SourceUri); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

당신은 다음과 같이 사용합니다 :

<Image> 
    <Image.Source> 
     <MultiBinding Converter="{StaticResource MultiValueConverter}"> 
      <Binding Path="SelectedExample"/> 
      <Binding Path="ValidationTest"/> 
     </MultiBinding> 
    </Image.Source> 
</Image> 

편집 컨버터가 자신의 파일에 2
, MultiValueConverter.cs 말, 그것은 어디 갈 것입니다 당신의 뷰는 바람직하게는 Converters 폴더에 있습니다. 태그이처럼 Window 또는 UserControl 자원의 내부 지금

xmlns:converter="clr-namespace:SO_app.Converters"//clr-namespace is the namespace of your view followed by a dot to access the Converters folder 

: 이제 상단에있는 XAML에서이 같은 것을 컨버터를 포함 할 것

모든 게임 타이틀을 가지고 있기 때문에 작동하지 않습니다
<Window.Resources> 
    <converter:MultiValueConverter x:Key="MultiValueConverter"/> 
</Window.Resources> 
+0

모든 게임에는 플랫폼이 있습니다. null 값이 없습니다. 멀티 바인딩이 실제로 게임 타이틀을 기반으로 이미지를 찾을 수있을 때만 바뀌는 기본 이미지를 보여주는 방법이 필요합니다. – Sweepster

+1

아, 죄송합니다. 하지만 당신은 "게임 타이틀".png가 존재하면 UI는 이미지 만 보여주고 있습니다. " 따라서 게임에 여러 개의 이미지 (멀티 플랫폼 게임)가있을 수 있지만 항상 적어도 하나는 있습니다. 따라서 사용 가능한 이미지의 수를 감지하고 둘 이상의 이미지가있는 경우 기본값을 선택하는 무언가를 갖고 싶습니까? – XAMlMAX

+0

아니요. 디렉터리 구조는/Themes/Custom/Images/Platform Name입니다. 이 폴더에는 Platform Name.png (이것이 기본 이미지가 될 것임)와 컨트롤러 유형이 다른 각 게임에 대한 하나의 이미지가 있습니다. 그 이미지들 각각은 그것이 묶여있는 의미의 게임과 같은 이름을 가질 것입니다. (예 : Super Mario.png, Kirby.png 등). 예를 들어, 게임에 Pacman이라는 게임이 있지만 거기에 Pacman.png가 없으면 대신 UI에 Platform Name.png을 표시하고 싶습니다. – Sweepster